Skip to content

Instantly share code, notes, and snippets.

Created October 14, 2012 00:59
Show Gist options
  • Save anonymous/3886805 to your computer and use it in GitHub Desktop.
Save anonymous/3886805 to your computer and use it in GitHub Desktop.
import java.util.*;
public class List {
private int[] array;
int items;
boolean full;
public List()
{
array = new int[10];
for(int i=0; i<10; i++)
array[i] =0;
items = 0;
full = false;
}
public List(int size)
{
array = new int[size];
items=0;
full = false;
}
public List(int[] arr)
{
array = arr;
items = arr.length;
full = true;
}
public int size()
{
return items;
}
public void print()
{
for(int i=0; i<10; i++)
{
System.out.printf("%3d", array[i]);
}
}
public void add(int num)
{
if(full != true)
{
for(int i=array.length-1; i>=0; i--)
{
array[i] = array[i-1];
}
}
else
{
int[] temp = Arrays.copyOf(array, array.length*2);
for(int i=temp.length-1; i>=0; i--)
{
temp[i] = temp[i-1];
}
array = Arrays.copyOf(temp, temp.length);
}
items+=1;
if(items == array.length)
full = true;
}
public void add(int num, int pos)
{
if(full != true)
{
for(int i=array.length; i>pos; i--)
{
array[i]= array[i-1];
array[pos] = num;
}
}
else
{
int[] temp = Arrays.copyOf(array, array.length*2);
for(int i=temp.length-1; i>=0; i--)
{
temp[i] = temp[i-1];
}
array = Arrays.copyOf(array, num);
}
items+=1;
if(items == array.length)
full = true;
}
}}
public class AssignTwo {
public static void main(String[] args)
{
List k = new List();
// for(int i=0; i<15; i++)
//{
// list.add(7);
//}
k.print();
System.out.println(k.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment