I have made an ADT called NumList, and have implemented it in a class NumArrayList
of the methods implemented, there is an insert(int i, double value) where value is inserted into array[i].
int numItems is a counter keeping track of my array's elements.
public void insert(int i, double value)
{
if (numItems >= items.length)
{
double[] tempItems = new double [items.length * 2];
for(int j =0 ; j < items.length; j++ )
{
tempItems[j] = items[j];
}
tempItems[items.length] = value;
items = tempItems;
}
else
{
if (i > numItems)
{
items[numItems] = value;
}
else
{
for (int k = i; k < numItems; k++)
{
items[k+1] = items[k];
}
items[i] = value;
}
}
numItems++;
}
is my method, looking simple enough.
public static void main (String[] args)
{
NumArrayList test;
test = new NumArrayList();
//System.out.println("this is how many initial items the initialized array has.");
//System.out.println(test.items);
test.insert(1, 0.1);
System.out.println("have tried to insert value 0.1 @ position 1, that is the second element in array.");
test.print();
is my test code area, built into the same class.
I'm receiving an error where the compiler claims I have an ArrayIndexOutOfBoundsException at line 47, or at
tempItems[items.length] = value;
I believe it's trying to tell me that my initialization of items is wrong,
private double[] items;
private int numItems;
public NumArrayList()
{
items = new double[0];
numItems = 0;
}
but the initialization has already been approved by a much better programmer than I, and these errors are leading me nowhere. Perhaps a nudge as to which part of the program I should look into?