用Java将表数据读入2D数组

rafiaTech:

有人可以帮我从文件中读取图形数据并将数据保存到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]);
            }
        }
    }

非常感谢您提供的任何帮助。

Butiri Dan:

您有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章