gpt4 book ai didi

java - 将 2D ArrayList 转换为 2D 字符串数组

转载 作者:行者123 更新时间:2023-12-02 06:49:45 27 4
gpt4 key购买 nike

我想转换 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 ArrayListparent”很容易转换,但“child”给出上述错误

最佳答案

这一行:

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

正在尝试创建 ArrayList 数组,因为 child.toArray() 将返回 ArrayList[] 作为异常状态

source[0] of type java.util.ArrayList cannot be stored in destination array of type 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 大小发生变化,您必须小心。

关于java - 将 2D ArrayList<String> 转换为 2D 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com