0

I have an arraylist inside an arraylist (nested arraylist) as below

  ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

Now I need to get an instance of the arraylist that exists in a given index of the indexOfJSONObject arraylist and add a value to it. I used the following code

    ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

    tempJSONObjectAL.add(value); 

but it gives me the error of

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

how to fix this and why this happening.

Thank you

8
  • It's not very hard to unserstand the error : You want the element at the index 0 , so the first BUT the list is empty : size = 0 You have apparently not provide any data to indexOfJSONObject Commented Feb 14, 2017 at 9:41
  • 1
    it's happeninng because indexOfJSONObject does not contain any items and per doc spec calling get on empty list results in IndexOutOfBoundsException Commented Feb 14, 2017 at 9:41
  • indexOfJSONObject is null at the time you are calling it Commented Feb 14, 2017 at 9:42
  • I guess you basically do not have anything within the first location of the arrayList :-) That's basically the reason your'e getting the error IOOBE. As adding an empty arraylist to an arraylist does not make any sense - as the first element will be an empty object :-) However, if you new ArrayList<> first, then add a new ArrayList<> after that, things will work as intended :-). Commented Feb 14, 2017 at 9:43
  • 1
    also maybe it would be more convinient and readable to use Map<Integer, List<Integer>> Commented Feb 14, 2017 at 9:44

3 Answers 3

2

The problem here seems that list size is 0 and you are still trying to access an element at position 0.

You should never try to directly access an element in a collection, without using a loop/iterator or without putting a check comparing size and givenIndex. Your code should be something like

ArrayList<Integer> tempJSONObjectAL= null;
if(indexOfJSONObject.size() > givenIndex)
    tempJSONObjectAL = (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);
Sign up to request clarification or add additional context in comments.

Comments

2

ReasonThis is very simple. This error is because indexOfJSONObject is an ArrayList which itself holds an ArrayList. But You haven't any ArrayList inside the indexOfJSONObject.
You are initially getting the ArrayList from indexOfJSONObject, while there is no ArrayList Instantiation inside it.
You need to add a new Instantiation of ArrayList to the indexOfJSONObject and then work with it.

By adding one specific statement will solve the issue. Just check my code below:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();

//This line of code is required in your case
indexOfJSONObject.add(new ArrayList<Integer>());

ArrayList<Integer> tempJSONObjectAL= (ArrayList<Integer>)indexOfJSONObject.get(givenIndex);

tempJSONObjectAL.add(value); 

Comments

1

Try the following code:

ArrayList<ArrayList<Integer>> indexOfJSONObject = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tempJSONObjectAL=new ArrayList<Integer>();

        for(ArrayList<Integer> list:indexOfJSONObject)
        {
            tempJSONObjectAL.add(list.get(index));

        }

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.