import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
class DynamicArrayOfInts
{
private int[] storage;
private int size;
private final int INITIAL_CAPACITY = 8;
private final int GROW_FACTOR = 2;
public DynamicArrayOfInts()
{
storage = new int[INITIAL_CAPACITY];
size = 0;
}
//check for out of bound exception
private void rangeCheck(int index)
{
if((index < 0)||(index == size)||(index > size))
{
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
else
{
System.out.println("Array is inbound");
}
}
//make sure array grow as there are more values to be added
private void ensureCapacity(int size_wanted)
{
int max_capacity = storage.length;
if (size_wanted > max_capacity)
{
max_capacity = max_capacity * GROW_FACTOR +1;
storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
}
}
//get size of the array
public int size()
{
size = storage.length;
return size; // added so code would compile
}
//compare aThat array with the current array
public boolean equals(Object aThat)
{ // aThat is a DynamicArrayOfInts object
//check if aThat is empty or not instance of Dynamic
if((aThat==null)||!(aThat instanceof DynamicArrayOfInts))
return false;
DynamicArrayOfInts aThatLocal = (DynamicArrayOfInts) aThat;
//compare length
if(storage.length != aThatLocal.size)
return false;
//compare elements
for (int i = 0;i<storage.length;i++)
{
if(storage[i]!=aThatLocal.get(i))
{
return false;
}
}
return true;
}
//compare list with current array
public boolean equals(List<Integer> list)
{ // list is a LinkedList, or ArrayList, etc
//check size
if(list.size()!=storage.length)
return false;
//compare elements
for (int i = 0;i<storage.length;i++)
{
if(storage[i] != list.get(i))
{
return false;
}
}
return true;
}
//get value at the specific index
public int get(int position)
{
rangeCheck(position);
return storage[position];
}
//set value at specific index
public void set(int index, int value)
{
rangeCheck(index);
storage[index]= value;
}
//insert value at specific index and shift everything from that index to the right
public void insertAt(int index, int value)
{
if ((index<0)||(index>size))
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
else
{
int theLength = storage.length;
int temp=0;
int j=theLength;
ensureCapacity(size+1);
for(int i = (theLength-1);i != index;i--)
{
temp = storage[i];
storage[j] = temp;
j--;
}
storage[index] = value;
}
}
//add value to the end of array
public void add(int value)
{
int endPosition = size;
insertAt(endPosition, value);
}
//remove
public void removeAt(int index)
{
rangeCheck(index);
if (index == 0 )
{
}
}
//print everything out
public void printAll()
{
for(int i = 0;i<storage.length;i++)
{
System.out.print("[" + i + "]=" + storage[i] + "\t" );
}
System.out.println();
}
public static void main(String args[])
{
DynamicArrayOfInts list1 = new DynamicArrayOfInts();
list1.insertAt(0,1);
list1.insertAt(1,2);
list1.add(3);
// list1 is 1, 2, 3
System.out.print("list1: "); list1.printAll();
list1.set(2,100);
// list1 1 is 1, 2, 100
//System.out.print("list1: "); list1.printAll();
System.out.println("list1[2]=" + list1.get(2));
list1.removeAt(2);
// list1 is 1, 2
System.out.print("list1: "); list1.printAll();
DynamicArrayOfInts list2 = new DynamicArrayOfInts();
list2.insertAt(0,2);
list2.insertAt(0,3);
list2.insertAt(0,1);
list2.removeAt(1);
// list2 is 1, 2
System.out.print("list2: ");list2.printAll();
System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
// list1 and list2 are equal
System.out.println("list1 equals list2 is " + list1.equals(list2));
list2.insertAt(2,3);
// list2 is 1, 2, 3
System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
// list1 and list2 are not equal
System.out.println("list1 equals list2 is " + list1.equals(list2));
ArrayList list3 = new ArrayList();
list3.add(1);list3.add(2);list3.add(3);
System.out.println("list2 equals list3 is " + list2.equals(list3));
}
}
removeAt(index) I have not yet doing it... I want to finish add(value) first before move to that method. So basically the add method would add a value at the end of the array, by calling insertAt(index,value) but I am not sure how to accomplish that...
For removeAt(index) method, I think there are 2 different cases. One is when I remove in the middle or first index of the array; second is remove the last member in the array. For the first case, if i remove the values, I can shift everything after that index to the left. For the second case, I have to have some check to verify that I am removing the last member of the array, so I dont need to do the shifting, what can I do to check that?

New Topic/Question
Reply



MultiQuote





|