我想将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
1D ArrayList
“父”正在轻松转换,但“孩子”正在提供上述错误
答案 0 :(得分:2)
这一行:
String[][] childString=child.toArray(new String[parent.size()][child.size()]);
正在尝试创建一个ArrayLists数组,因为child.toArray()
会将ArrayList[]
作为异常状态返回
java.util.ArrayList类型的source [0]无法存储在java.lang.String [] []
类型的目标数组中
要执行此操作,您需要创建数组和循环以填充
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;
}
如果ArrayList大小不同,你必须要小心。