[英]Storing ArrayList Data and Converting to a String[][]
我想我的代码是正确的,用于在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 [] []类型。 关于我能做什么的任何想法?
编辑:所以转换问题解决了(编辑过的代码粘贴在上面) - 现在它输出什么都没有。 我觉得我必须错误地存储数据。 我应该添加/更改什么?
该数组将是String[]
的Object[]
String[]
,因此强制转换不起作用。 您可以(并且应该)使用提供数组的toArray()
版本来填充:
String[][] numbers = (String[][]) al.toArray(new String[al.size()][]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.