Search code examples
javaandroidmultidimensional-arrayindexoutofboundsexception

2D ArrayList Index Out of Bounds


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)));
    }
}

Solution

  • 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.