[英]Storing ArrayList Data and Converting to a String[][]
I think I have the code correct for storing data in an ArrayList. 我想我的代码是正确的,用于在ArrayList中存储数据。 However, I can't convert the list to a String[][].
但是,我无法将列表转换为String [] []。 Here's my code:
这是我的代码:
int row = 0;
int col = 0;
String fileInput = JOptionPane.showInputDialog(null,
"Please enter the path of the CSV file to read:");
File file = new File(fileInput);
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;
// Construct a new empty ArrayList for type String
ArrayList<String[]> al = new ArrayList<String[]>();
//read each line of text file
while((line = bufRdr.readLine()) != null) {
String[] columnData = line.split(",");
al.add(columnData);
}
// Convert ArrayList to String array
String[][] numbers = (String[][]) al.toArray(new String[al.size()][16]);
I'm getting an error stating I can't convert to a String[][] type. 我收到一条错误,指出我无法转换为String [] []类型。 Any ideas on what I can do?
关于我能做什么的任何想法?
EDIT: So the conversion problem is solved (the edited code is pasted above) - now its outputting nothing though. 编辑:所以转换问题解决了(编辑过的代码粘贴在上面) - 现在它输出什么都没有。 I feel like I must have stored the data incorrectly.
我觉得我必须错误地存储数据。 What should I add/change?
我应该添加/更改什么?
The array will be an Object[]
of String[]
, so the cast doesn't work. 该数组将是
String[]
的Object[]
String[]
,因此强制转换不起作用。 You can (and should) use the toArray()
version that supplies the array to populate: 您可以(并且应该)使用提供数组的
toArray()
版本来填充:
String[][] numbers = (String[][]) al.toArray(new String[al.size()][]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.