Converting 2D ArrayList to 2D String Array

I want to convert a 2D ArrayList<String> to 2D String Array. Here is the code

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<String> parent = new ArrayList<String>();
    ArrayList<String> child1 = new ArrayList<String>();
    ArrayList<ArrayList<String>> child = new ArrayList<ArrayList<String>>();
    for (int i = 0; i < 10; i++) {
        parent.add("" + i);
        for (int j = 0; j <= i; j++) {
            child.add(new ArrayList<String>());
            child.get(i).add("" + j);
        }
    }
    System.out.println("asdasd"+child.size());
    String[] parentString=parent.toArray(new String[parent.size()]);
    String[][] childString=child.toArray(new String[parent.size()][child.size()]);

    System.out.println("parent output");
    for(int i =0; i<parentString.length ;i++){
        System.out.println(parentString[i]);
    }
    System.out.println("Child output");
    for(int i=0;i<parentString.length;i++){
        for(int j=0;j<child.get(i).size();j++){
            System.out.println(childString[i][j]);
        }
    }
    System.out.println("done");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

As you can see that i am trying to convert “child” ArrayList to “childString” String. Here is the LogCat

08-11 16:03:00.306: E/AndroidRuntime(21581): FATAL EXCEPTION: main
08-11 16:03:00.306: E/AndroidRuntime(21581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.darraylist/com.example.darraylist.MainActivity}: java.lang.ArrayStoreException: source[0] of type java.util.ArrayList cannot be stored in destination array of type java.lang.String[][]
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.os.Looper.loop(Looper.java:137)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread.main(ActivityThread.java:5039)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at java.lang.reflect.Method.invoke(Method.java:511)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at dalvik.system.NativeStart.main(Native Method)
08-11 16:03:00.306: E/AndroidRuntime(21581): Caused by: java.lang.ArrayStoreException: source[0] of type java.util.ArrayList cannot be stored in destination array of type java.lang.String[][]
08-11 16:03:00.306: E/AndroidRuntime(21581):    at java.lang.System.arraycopy(Native Method)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at java.util.ArrayList.toArray(ArrayList.java:519)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at com.example.darraylist.MainActivity.onCreate(MainActivity.java:28)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.Activity.performCreate(Activity.java:5104)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-11 16:03:00.306: E/AndroidRuntime(21581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-11 16:03:00.306: E/AndroidRuntime(21581):    ... 11 more

1D ArrayListparent” is being easily converted but “child” is giving above error

Answer

This line:

String[][] childString=child.toArray(new String[parent.size()][child.size()]);

is trying to create an array of ArrayLists as child.toArray() will return ArrayList[] as exception states

source[0] of type java.util.ArrayList cannot be stored in destination array of type java.lang.String[][]

To do this you will need to create array and loop to populate

String[][] childString = new String[parent.size()][child.size()];
int i = 0;
int j = 0;
for(ArrayList<String> al : child)
{
    for(String s: al)
    {
        childString[i][j] = s;
        j++;
    }
    i++;
    j = 0;
}

You will have to be wary if the ArrayList sizes vary.

Leave a Reply

Your email address will not be published. Required fields are marked *