【发布时间】:2010-12-16 02:28:38
【问题描述】:
大家好,这是my previous question 的后续活动。我现在有一个格式如下的文本文件:
100 200
123
124 123 145
我想要做的是将这些值放入 Java 中的二维参差不齐的数组中。 我到目前为止是这样的:
public String[][] readFile(String fileName) throws FileNotFoundException, IOException {
String line = "";
ArrayList rows = new ArrayList();
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null) {
String[] theline = line.split("\\s");//TODO: Here it adds the space between two numbers as an element
rows.add(theline);
}
String[][] data = new String[rows.size()][];
data = (String[][])rows.toArray(data);
//In the end I want to return an int[][] this a placeholder for testing
return data;
我的问题是,例如对于第 100 200 行,变量“theline”具有三个元素 {"100","","200"},然后将其传递给带有 rows.add(theline) 的行
我想要的是只有数字,如果可能的话,如何将这个 String[][] 数组转换为 int[][] 数组 int 最后返回它。
谢谢!
【问题讨论】:
-
你可能会在这个讨论中找到一些灵感stackoverflow.com/questions/691184/…
标签: java arrays string file arraylist