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
But when I tried to add something to the _items ArrayList, I was getting ArrayIndexOutOfBoundsExceptioncould you show us how you ware trying to add something to your list?this._items.add(index, Item)Since there was nothing in the ArrayList, size would be 0 and I was getting ArrayIndexOutOfBoundsException.ArrayListin 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 :)