存储 ArrayList 数据并转换为 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[][] 类型。关于我能做什么有什么想法吗?
编辑:所以转换问题解决了(编辑后的代码粘贴在上面) - 现在它什么也不输出。我觉得我一定是错误地存储了数据。我应该添加/更改什么?
I think I have the code correct for storing data in an ArrayList. However, I can't convert the list to a 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该数组将是
String[]
的Object[]
,因此强制转换不起作用。您可以(并且应该)使用提供要填充的数组的toArray()
版本:The array will be an
Object[]
ofString[]
, so the cast doesn't work. You can (and should) use thetoArray()
version that supplies the array to populate: