java - Converting 2D ArrayList<String> to 2D String Array -
i want convert 2d arraylist<string>
2d string array
. here 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>>(); (int = 0; < 10; i++) { parent.add("" + i); (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 =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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }
}
as can see trying convert "child" arraylist
"childstring" string
. here 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 start activity componentinfo{com.example.darraylist/com.example.darraylist.mainactivity}: java.lang.arraystoreexception: source[0] of type java.util.arraylist cannot stored in destination array of type java.lang.string[][] 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread.access$600(activitythread.java:141) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 08-11 16:03:00.306: e/androidruntime(21581): @ android.os.handler.dispatchmessage(handler.java:99) 08-11 16:03:00.306: e/androidruntime(21581): @ android.os.looper.loop(looper.java:137) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread.main(activitythread.java:5039) 08-11 16:03:00.306: e/androidruntime(21581): @ java.lang.reflect.method.invokenative(native method) 08-11 16:03:00.306: e/androidruntime(21581): @ java.lang.reflect.method.invoke(method.java:511) 08-11 16:03:00.306: e/androidruntime(21581): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-11 16:03:00.306: e/androidruntime(21581): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-11 16:03:00.306: e/androidruntime(21581): @ 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 stored in destination array of type java.lang.string[][] 08-11 16:03:00.306: e/androidruntime(21581): @ java.lang.system.arraycopy(native method) 08-11 16:03:00.306: e/androidruntime(21581): @ java.util.arraylist.toarray(arraylist.java:519) 08-11 16:03:00.306: e/androidruntime(21581): @ com.example.darraylist.mainactivity.oncreate(mainactivity.java:28) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activity.performcreate(activity.java:5104) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 08-11 16:03:00.306: e/androidruntime(21581): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 08-11 16:03:00.306: e/androidruntime(21581): ... 11 more
1d arraylist
"parent" being converted "child" giving above error
this line:
string[][] childstring=child.toarray(new string[parent.size()][child.size()]);
is trying create array of arraylists child.toarray()
return arraylist[]
exception states
source[0] of type java.util.arraylist cannot stored in destination array of type java.lang.string[][]
to need create array , loop populate
string[][] childstring = new string[parent.size()][child.size()]; int = 0; int j = 0; for(arraylist<string> al : child) { for(string s: al) { childstring[i][j] = s; j++; } i++; j = 0; }
you have wary if arraylist sizes vary.
Comments
Post a Comment