我正在尝试将如下所示的文本文件存储到2D数组中。
24
7
1 6
7 15
13 17
9 23
10 21
13 19
这是我必须存储的代码。没有语法错误,但我似乎无法将其存储到我的数组中。我只需要弄清楚如何将它存储到我的数组中。
FileInputStream fStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(fStream));
Scanner src = new Scanner(input);
ArrayList<Integer> lines = new ArrayList<Integer>();
src.useDelimiter(" ");
while(src.hasNextInt()) {
lines.add(src.nextInt());
}
int[][] fileArray = new int[lines.size()][];
lines.toArray(fileArray);
-Thanks Mike
答案 0 :(得分:2)
试一试
FileInputStream fStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(fStream));
Scanner src = new Scanner(input);
ArrayList<ArrayList<Integer>> board = new ArrayList<>();
while(src.hasNextLine())
{
String[] line = src.nextLine().split("\\s+");
board.add(new ArrayList<Integer>());
for(String num : line)
{
if(num.matches("\\d+"))
board.get(board.size() - 1).add(Integer.parseInt(num));
}
}
for(int i = 0; i < board.size(); i++)
{
for(int j = 0; j < board.get(i).size(); j++)
System.out.print(board.get(i).get(j) + " ");
System.out.println();
}