0

I was implementing 2D Array for my APP.
But unfortunately I had this error.

FATAL EXCEPTION: main
   Process: com.application.recast, PID: 18534
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.recast/com.application.recast.SplashScreen}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6077)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
       at java.util.ArrayList.get(ArrayList.java:411)
       at com.application.recast.SplashScreen.onCreate(SplashScreen.java:60)
       at android.app.Activity.performCreate(Activity.java:6664)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:154) 
       at android.app.ActivityThread.main(ActivityThread.java:6077) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

What I am doing is that, I want to store specific item into specific index of 2D array.
Pretty new with 2D arraylist.
Here's my code.

ArrayList<String> mStrings = new ArrayList<>();
ArrayList<ArrayList<String>> list = new ArrayList<>();

mStrings.add("HI");
mStrings.add("HI2");
mStrings.add("HI3");
mStrings.add("HI4");
list.add(0,mStrings);

for(int x=0;x<list.size();x++){
    for(int y=0;y<mStrings.size();y++){
        Log.d("VALUE",String.valueOf(list.get(y)));
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Charles Galvez
  • 1,100
  • 5
  • 19
  • 41

6 Answers6

3

Issue java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at

list has only 1 element mean list.get(0) but for(int y=0;y<mStrings.size();y++){ will be executed 4 time hence error here list.get(y)

list contains only single element i.e. mStrings

and further for single element in list this logic works but for multiple elements in list you need to fetch the element from list then traverse it

for(int x=0;x<list.size();x++){
         ArrayList<String> sublist = list.get(x);
            for(int y=0;y<sublist.size();y++){
                //Log.d("VALUE",String.valueOf(sublist.get(y)));
                Log.d("VALUE at index "+y,sublist.get(y));
            }
        }

Note : now String.valueOf( is unnecessary because your data is string type

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

It may help to draw a hypothetical picture of a List of List. Here is one possible outer list, and its contents:

0: []
1: [a, b, c]
2: [d, e]
3: []

etc.

So, it is unlike a 2D array in that there is no uniform column size.

So you can see, the outer list at index 0 is empty, at index 1, there is a list with 3 items, etc.

Given this, to iterate over a List<List<Object>>, you'll have to check the size of the inside list each time as well:

for (int x = 0; x < list.size(); x++) {
    for (int y = 0; y < list.get(x).size(); y++) {
        Log.d("VALUE", String.valueOf(list.get(x).get(y)));
    }
}

Where list.get(x) itself return a list, and list.get(x).get(y) return a value in the sublist.

Update: or as @cricket_007 points out, you can just use the for-each style loop, for more readable code.

Jameson
  • 6,400
  • 6
  • 32
  • 53
1

You are iterating mString ArrayList size with list ArrayList

Conditions

If you have mString ArrayList size is greater than list ArrayList then there will be IndexOutOfBoundsException

Try this

for(int x=0;x<list.size();x++){
Log.d("VALUE",String.valueOf(list.get(x)));
    for(int y=0;y<mStrings.size();y++){
        Log.d("VALUE2",String.valueOf(mStrings.get(x)));
    }

}
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
1

Do you need the index?

If not, you can use an enhanced for loop.

for(List<String> innerList : mStrings){
    for(String s : innerList){
        Log.d("VALUE", s);
    }
}

Your error is that list.size() != mStrings.size()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

Since you are using 2D array list, So in implementation part, What You need to do is

ArrayList<T> A = new ArrayList();
ArrayList<ArrayList<T>> B = new ArrayList();

This means Arraylist B is storing Arraylist A in each of there index. So for getting that stored element in B. You need to do like this.

ArrayList<T> A = B.get(i); // this will return the list stored at index i or you can say ith row.

//now
T a = A.get(j); //this will return the item stored index jth.

//in one line
T a = (B.get(i)).get(j); // this will return the value stored at ith row and jth column. 
Ashish M
  • 763
  • 6
  • 13
1

This is your code

ArrayList<String> mStrings = new ArrayList<>();
ArrayList<ArrayList<String>> list = new ArrayList<>();

mStrings.add("HI");
mStrings.add("HI2");
mStrings.add("HI3");
mStrings.add("HI4");
list.add(0,mStrings);

for(int x=0;x<list.size();x++){
    for(int y=0;y<mStrings.size();y++){
        Log.d("VALUE",String.valueOf(list.get(y)));
    }
}

list has a single element, yet you try to reach its y'th element, when y will get bigger due to the size of mStrings. You will need list.get(x).get(y) instead of list.get(y). It is better to define your inner for dynamically, to cope with the situation when the outer list has more elements:

ArrayList<String> mStrings = new ArrayList<>();
ArrayList<ArrayList<String>> list = new ArrayList<>();

mStrings.add("HI");
mStrings.add("HI2");
mStrings.add("HI3");
mStrings.add("HI4");
list.add(0,mStrings);

for(int x=0;x<list.size();x++){
    for(int y=0;y<list.get(x).size();y++){
        Log.d("VALUE",String.valueOf(list.get(x).get(y)));
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175