谁能帮我从文件中读取图形数据并将数据保存到 Java 二维数组或列表中?我一直在努力
这是我目前的代码:
Scanner matrix = new Scanner(new File("graph_input.txt"));
String[][] arr = new String[8][];
while(matrix.hasNextLine()){
String[] data = matrix.nextLine().split("\\s+");
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[i].length; j++){
arr[i][j] = Arrays.toString(arr[j]);
}
}
}
非常感谢您提供的任何帮助。
最佳答案
你有一个 while
和 2 个 for
。你只需要一个 for
Scanner matrix = new Scanner(new File("graph_input.txt"));
// Base on this you have 8 line in the matrix
String[][] arr = new String[8][];
// Read all 8 lines
for (int i = 0; i < arr.length; i++) {
// Get the elements of line i
arr[i] = matrix.nextLine().split("\\s+");;
}
关于java - 在 Java 中将表数据读入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938866/