2

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?

user1702633
  • 111
  • 3
  • 5
  • 8

4 Answers4

2

Your initialization is certainly wrong. What is a reasonable default size? For ArrayList the answer is 10. You can make it whatever you like, but not zero! If you double the length of an array with size 0, the new array still has length 0.

int capacity; //stores the size of the array (items available)
int numItems; //stores how many items are actually stored in the array.

public NumArrayList()  {
    items = new double[10];
    numItems = 0;
    capacity = 10;
}
Thorn
  • 3,935
  • 4
  • 20
  • 42
  • hm I guess that is it, my tutor and I were understanding that since we were changing the size of the array by copy/pasting into new ones upon lacking array size, initialization to 0 was fine...fail. – user1702633 Sep 27 '12 at 22:40
0

You have to remember that array always starts with index 0 and not 1. So if your array size is 10, the maximum index is 9 and not 10.

tempItems[0] = first element;
tempItems[1] = second element;

etc etc

Assuming you have 10 elements, your tenth element will be in tempItems[9]. Trying to access tempItems[10] will throw the exception you're seeing. Basically if you're looking for the last index, you want to do:

tempItems[items.length-1] = value;

EDIT: Forget this. You're doubling your array index on initialization. Refer to Thorn's post above.

Michael
  • 2,584
  • 2
  • 17
  • 17
  • This is true, but in the posted code, tempItems.length is equal to items.length*2, so tempItems[items.length] is not really a problem. Unless of course, both of these are zero as indicated by the constructor. – Thorn Sep 27 '12 at 22:32
0

change this

    tempItems[items.length] = value;

to

    tempItems[items.length-1] = value;

array index starts with 0, if your array is of length 5, your last index would be 4

PermGenError
  • 43,905
  • 8
  • 81
  • 104
0

Once you allocate places to an array , lets say items = new double[0]; then you cannot change the size of the array. If the array is initialised to 0, that means you have a useless array. Anything you add to it, it will throw Array index out of bounds exception.

The way to go is by using Collections and specifically the List interface.

List myList = new List(); //Create an empty list
myList.add(item); //adds item to List

There are also other implementations of List, like ArrayList, LinkedList etc... that can fit better your needs .

MaVRoSCy
  • 16,907
  • 14
  • 76
  • 116