Secret
Secret

Reputation: 3358

Java Out Of Range Error

Why does the following return an IndexOutOfBoundsException? (Index 5, size 0)

gridList = new ArrayList<Integer>(9);
gridList.add(5, 2);

I was under the impression that the constructor call initializes my arraylist to the size.

I'm quite new with java, so apologies.

Upvotes: 3

Views: 1514

Answers (5)

ashatte
ashatte

Reputation: 5538

Calling that constructor just specifies an initial capacity, but has no effect on the size of the ArrayList (size will always be zero until you add anything). This is explained in the docs and also evidenced by printing out that ArrayList:

ArrayList<Integer> gridList = new ArrayList<Integer>(9);
System.out.println(gridList);

Output: []

If you want to initialize an ArrayList with 9 Integers (for example, nine zeroes), try this 'convenience implementation':

ArrayList<Integer> gridList = new ArrayList<Integer>(Collections.nCopies(9, 0));
System.out.println(gridList);

Output: [0, 0, 0, 0, 0, 0, 0, 0, 0]

As you can see this fills the ArrayList with values during initialization, so you can now call gridList.add(5, 2); without an IndexOutOfBoundsException.

Upvotes: 6

Paul Samsotha
Paul Samsotha

Reputation: 209052

It's still an empty list (size 1, only index available is 0). Just the capacity is 9. Once the capacity has reached capacity, the ArrayList will expand.

Note: size and capacity are two different thing.

Upvotes: 2

Artur
Artur

Reputation: 7257

As others have pointed out in ths case you are entering en alement at position 5 but you have nothing there and there are no elements before - look here:

    ArrayList<Integer>  g = new ArrayList<Integer>(9);
    g.add(0, 10);
    g.add(1, 20);
    g.add(2, 30);
    g.add(3, 40);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(2,99);

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

    g.add(88); // use this to just push value at the end

    for(Integer v: g) System.out.print(v + " ");
    System.out.println();

yields:

10 20 30 40 
10 20 99 30 40 
10 20 99 30 40 88 

Upvotes: 1

Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Please follow the source code of ArrayList, you can see that size and capacity are different concept. checkBoundInclusive() method compares index with size not capacity.

public ArrayList(int capacity)
   {
     // Must explicitly check, to get correct exception.
     if (capacity < 0)
       throw new IllegalArgumentException();
     data = (E[]) new Object[capacity];
   }


   public void add(int index, E e)
   {
      checkBoundInclusive(index);
      modCount++;
      if (size == data.length)
        ensureCapacity(size + 1);
     if (index != size)
        System.arraycopy(data, index, data, index + 1, size - index);
      data[index] = e;
      size++;
    }


    private void checkBoundInclusive(int index)
    {
      // Implementation note: we do not check for negative ranges here, since
      // use of a negative index will cause an ArrayIndexOutOfBoundsException,
      // a subclass of the required exception, with no effort on our part.
      if (index > size)
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
                                            + size);
    }

Upvotes: 3

LaurentG
LaurentG

Reputation: 11787

The ArrayList is initialized with a capacity of 9 but the list is empty. Thus you cannot add an element at the position 5 since this position does not exist in the list.

Upvotes: 4

Related Questions