我想将 2D 转换ArrayList<String>
为 2D String Array
。这是代码
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;
}
}
如您所见,我正在尝试将"child"转换ArrayList
为 "childString"String
。这里是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
一维ArrayList
“父母”很容易转换,但“孩子”给出了上述错误