3

I was trying to initialize an ArrayList in a constructor, and I have something like this:

public class A {

private ArrayList<Items> _items = null;
private ArrayList<Items> _collection = null;

public A(int size1, int size2) { 
    this._items = new ArrayList<Items>(size1 * size2);
}

public void create(int size) {
    this._collection = new ArrayList<Items>(size);
}

But when I tried to add something to the _items ArrayList, I was getting ArrayIndexOutOfBoundsException. Could you explain this, please? I was searching for it and added this._itemsinstead of _items. I thought I going out of borders, but I tried to print _items.size and was getting 0, instead of size1 * size2...

I left the ArrayList = null because the size depends on the size1/size2 coming from the constructor

5
  • 1
    Can you post the code on how did you try to add to list? Commented Nov 30, 2013 at 11:55
  • But when I tried to add something to the _items ArrayList, I was getting ArrayIndexOutOfBoundsException could you show us how you ware trying to add something to your list? Commented Nov 30, 2013 at 11:55
  • As @Steve P. said, I was adding with a specific Index. Like: this._items.add(index, Item) Since there was nothing in the ArrayList, size would be 0 and I was getting ArrayIndexOutOfBoundsException. Commented Nov 30, 2013 at 12:09
  • Simply search in Google, "ArrayList in java." I'm quite sure that you yourself will be able to find it very simple within no time and solve the problem in question yourself :) Commented Nov 30, 2013 at 12:19
  • Well, I actually have the Java documentations opened, and when I wrote the program, I was looking for the methods I could use with an ArrayList... Commented Nov 30, 2013 at 12:21

2 Answers 2

2

Here's the source from ArrayList:

The constructor:

public ArrayList(int initialCapacity) 
{
     super();

     if (initialCapacity < 0)
          throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
     this.elementData = new Object[initialCapacity];
}

You called add(int, E):

public void add(int index, E element) {
     rangeCheckForAdd(index);
     ensureCapacity(size+1);  // Increments modCount!!
     System.arraycopy(elementData, index, elementData, index + 1, size - index);
     elementData[index] = element;
     size++;
 }

add calls rangeCheckForAdd(int):

 private void rangeCheckForAdd(int index) {
     if (index > size || index < 0)
         throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 }

It may be subtle, but when you called the constructor, despite initializing an Object[], you did not initialize size. Hence, from rangeCheckForAdd, you get the IndexOutOfBoundsException, since size is 0.

All you did by passing the argument to the constructor was set the capacity, which sets the size of the array that backs the ArrayList.

Instead of using add(int, E), you can use add(E e) and this won't occur.

Sign up to request clarification or add additional context in comments.

10 Comments

So, how should I inicialize it? this._items = new ArrayList(size1*size2) ?
@msk Your initialization is fine. You don't want to use the raw type as in the comment you just made. You are just unable to use set() unless there is already an element in the location. You can use the add method as I suggested, ie _items.add(myItem).
Oh, I think I got it. I had an algorithm that was adding in specific locations, so I wouldnt need to go through all the array to find a specific Item. I can't do that, can I?
@msk I assume that you're using the set method to try to add elements. The issue is that the way ArrayList is implemented, you can't use set unless an object is already in that location. To initially populate the ArrayList, you can simply use the add method, which will add your object to the current index, where index starts at 0 and increments each time you add to the list, but that's all handled for you in the background.
@msk If after you've added elements to the ArrayList, and you know that the element that you want is at index 2, the you could simply call the get(2) to retrieve the element or set(2, myItem) to replace it.
|
0

You should read the javadocs for this. size is not about 'how much the list can store' but about 'how many objects is it holding when the size was called'.

Since Lists (and for that matter any collection) are designed to grow dynamically with data being put into them, so it makes no sense to ask for the capacity of the list. If you are interested in capacity related introspection you should work with arrays instead.

Lists follow simple philosophical operations add an object, get an object, remove an object and iterate over all objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.