TimeToCodeTheRoad
TimeToCodeTheRoad

Reputation: 7322

ArrayList out of bounds exception

I have the following code:

ArrayList<Integer> arr = new ArrayList<Integer>(10);
arr.set(0,5);

I am getting an index out of bounds error, and I don't know why. I have declared the ArrayList of size 10. Why do I get this error?

Upvotes: 8

Views: 31688

Answers (10)

Muhammad Saqib Javed
Muhammad Saqib Javed

Reputation: 303

You have initialized arraylist with number of items 0 which means you just have 1 element in this array. and later you adding two numbers in this array.

Upvotes: 0

hage
hage

Reputation: 6163

You declared an ArrayList, that has the initial capacity of 10 elements, but you did not add an element to this list, i.e. the list is empty. set will replace an existing element, but since there is no element in the list, the exception is thrown. You have to add elements before, using the add method.

Initial capacity means that the array that the list maintains internally is of size 10 at the beginning. While adding more elements to the list, the size of this internal array might change.

Upvotes: 9

tobiasbayer
tobiasbayer

Reputation: 10389

In the constructor, you have specified an initial capacity. However, the size of the list is still 0 because you have not added any elements yet.

From the documentation of ArrayList.set():

IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

Upvotes: 0

Paul Rubel
Paul Rubel

Reputation: 27252

If you look at the javadoc note that it says:

Replaces the element at the specified position in this list with the specified element.

You need an element before you can replace one. Try

arr.add(5);

to just add an element.

Upvotes: 0

xyz
xyz

Reputation: 201

set(int index, E element) Replaces the element at the specified position in this list with the specified element. U should use add()

Upvotes: 0

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76886

From the documentation:

Constructs an empty list with the specified initial capacity.

(emphasis mine)

Upvotes: 2

Prince John Wesley
Prince John Wesley

Reputation: 63708

Use arr.add(0.5). set method will replace the existing element.

Upvotes: 0

quaylar
quaylar

Reputation: 2635

What you passed in the constructor is just the initial capacity of the array by which the list is backed. The list is still empty after construction. Moreover you should consider using a generic list if you want to store i.e. just Integers..

Upvotes: 1

Victor Sorokin
Victor Sorokin

Reputation: 12006

looking into JDK source code of ArrayList.set(int, E) gives a hint: you need to have at least N elements in your list before you can call set(N-1, E).

Add your elements via add() method.

Upvotes: 4

Dave G
Dave G

Reputation: 9777

The initial array contained is specified as "10" the actual number of items in the array is 0.

To add "5" at the first index, just do arr.add(5)

The value passed to the ArrayList constructor is the initial capacity of the backing store of the array. When you add elements to a point that exceeds this capacity, internally the ArrayList will allocate a new storage array of a size and copy the items to the new backing store array.

Upvotes: 3

Related Questions