有人可以帮我从文件中读取图形数据并将数据保存到Java 2D数组或列表中吗?我一直在努力
这是我到目前为止的代码:
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+");;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句