【发布时间】:2011-10-22 11:20:25
【问题描述】:
我认为我的代码可以正确地将数据存储在 ArrayList 中。但是,我无法将列表转换为 String[][]。这是我的代码:
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]);
我收到一条错误消息,指出我无法转换为 String[][] 类型。关于我能做什么的任何想法?
编辑:所以转换问题解决了(上面粘贴了编辑的代码) - 现在它什么也没输出。我觉得我一定是错误地存储了数据。我应该添加/更改什么?
【问题讨论】:
标签: java string multidimensional-array arraylist