【发布时间】:2014-10-28 00:02:27
【问题描述】:
我需要将 Array List 转换为 Object[][],我尝试了几种不同的方法,但似乎都抛出了一两个错误。
我最近的尝试是这样的:
Object[][] array = dataList.toArray(new Object[dataList.size()][]);
这会引发以下错误:
java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(Unknown Source)
我的数组列表中填充了我创建的类,这是类:
class dataClass {
int x;
int y;
int z;
String string1;
String string2;
Date date;
int event;
public dataClass(int x, int y, int z, String string1, String string2,
Date date, int event) {
this.x = x;
this.y = y;
this.z = z;
this.string1 = string1;
this.string2 = string2;
this.date = date;
this.event = event;
}
}
这就是我初始化数组列表的方式:
public static List<dataClass> dataList = new ArrayList<dataClass>();
然后我通过以下方式添加我的新数据类:
.add(new dataClass(...));
任何帮助都会很棒,谢谢。
【问题讨论】:
-
您希望
ArrayList中的元素如何存储在Object[][]中? -
我看到一个额外的维度突然出现,没有明显的原因,你能详细说明吗?
-
平原人民需要一个答案
-
在 Java 中,类名以大写字母开头是惯例。请查看 Java 教程:Variables and Naming。
-
@ScubaSteve 我假设
dataList是java.util.ArrayList。
标签: java arrays object arraylist