Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用字符串中的输入填充二维数组的行_Java_Arrays_Matrix_Multidimensional Array - Fatal编程技术网

Java 用字符串中的输入填充二维数组的行

Java 用字符串中的输入填充二维数组的行,java,arrays,matrix,multidimensional-array,Java,Arrays,Matrix,Multidimensional Array,我有以下问题: 矩阵中每一行的值都是给定的,列之间用空格分隔——因此我将所有行值输入字符串数组,删除空格,并将数字解析为int数组。现在,每行的值看起来像一个数字“12345”,而它们应该是“12345” 如何先将数字分开,然后通过向每行添加元素来填充矩阵?谢谢 这是我的密码: String n1 = input.nextLine (); int n = Integer.parseInt(n1); //rows of the matrix String[] arr = n

我有以下问题: 矩阵中每一行的值都是给定的,列之间用空格分隔——因此我将所有行值输入字符串数组,删除空格,并将数字解析为int数组。现在,每行的值看起来像一个数字“12345”,而它们应该是“12345”

如何先将数字分开,然后通过向每行添加元素来填充矩阵?谢谢 这是我的密码:

    String n1 = input.nextLine ();
    int n = Integer.parseInt(n1); //rows of the matrix
    String[] arr = new String [n]; //contains all the rows of the matrix
    int [] array = new int [arr.length]; // contains all the elements of the rows of the matrix without whitespace

    for (int i = 0; i < arr.length; i++) {
        arr [i] = input.nextLine().replaceAll("\\s+","");
        array[i] = Integer.parseInt(arr[i]);
    }

    int matrix [][] = new int [n][arr[0].length()];
String n1=input.nextLine();
int n=Integer.parseInt(n1)//矩阵的行数
字符串[]arr=新字符串[n]//包含矩阵的所有行
int[]数组=新的int[arr.length];//包含不带空格的矩阵行的所有元素
对于(int i=0;i
您应该
split()

示例如何将
String
转换为
String
数组(使用
split()
方法)


这里有一些重要问题:

for (int i = 0; i < arr.length; i++) {
    arr [i] = input.nextLine().replaceAll("\\s+",""); // loses the separator between the number
    array[i] = Integer.parseInt(arr[i]); // makes no sense as you want get all numbers submitted for the current row and no a single one
}
for(int i=0;i
如果在提交的每一行填充矩阵,则可以使用更少的变量进行处理。
没有经过测试的代码,但你应该有一个想法

String n1 = input.nextLine();
int n = Integer.parseInt(n1); //rows of the matrix  

int matrix [][] = null; // init it later : as you would have the two dimensions knowledge

for (int i = 0; i < n; i++) {
    String[] numberToken = input.nextLine().split("\\s"); 

    // matrix init : one time
    if (matrix == null){ matrix [][] = new int[n][numberToken.length]; }

    // array of int to contain numbers of the current row
    int[] array = new int[numberToken.length];

    // map String to int. Beware exception  handling that you should do
    for (int j = 0; j < numberToken.length; j++){
        array[j] = Integer.parseInt(numberToken[j]); 
    }
    // populate current row of the matrix
    matrix[i] = array[j];
}
String n1=input.nextLine();
int n=Integer.parseInt(n1)//矩阵的行数
int矩阵[][]=null;//稍后再开始:因为你会有二维知识
对于(int i=0;i
很难说,但据我所知,您正试图通过扫描仪逐行输入矩阵。 这可以解决你的问题

    Scanner scanner = new Scanner(System.in);
    //number of rows
    int n = Integer.parseInt(scanner.nextLine());
    int[][] matrix = new int[n][];
    for(int i=0;i<n;i++) {
        String line = scanner.nextLine();
        String[] numbers = line.split(" ");
        matrix[i] = new int[numbers.length];
        for(int j=0;j<numbers.length;j++) {
            matrix[i][j] = Integer.parseInt(numbers[j]);
        }
    }
Scanner Scanner=新的扫描仪(System.in);
//行数
int n=Integer.parseInt(scanner.nextLine());
int[][]矩阵=新的int[n][];

对于(int i=0;i)您的描述与您的代码不匹配。是的,这正是我试图做的,但我仍在努力解析动态输入的元素。您的代码帮助很大,我现在更了解了。谢谢!:)谢谢您的想法!int矩阵[][]=null的部分;非常有帮助,我不知道这是可能的:)你仍然不在。天哪,那是一个很长的假期。我希望你做得很好。。。无关者:如果你有一两分钟的时间,请看一下并让我知道你对这个问题的两个答案的看法。@GhostCat你好。是的,很好,你好吗?的确,这是一个很长的假期。在接下来的几个月里,我可能也会很少出席。所以,如果你想问我什么,就把我的电子邮件发到gmail.com上。关于这件事,你的要求总是有效的吗?很高兴收到你的来信。你收到邮件了。我希望这能奏效。我已经收到了关于这个答案的反馈,但请随意评论。当别人说他们知道得更好时,我总是尽力去理解。我明白了,我会回答的。顺便说一下,谢谢你的评论:)
String n1 = input.nextLine();
int n = Integer.parseInt(n1); //rows of the matrix  

int matrix [][] = null; // init it later : as you would have the two dimensions knowledge

for (int i = 0; i < n; i++) {
    String[] numberToken = input.nextLine().split("\\s"); 

    // matrix init : one time
    if (matrix == null){ matrix [][] = new int[n][numberToken.length]; }

    // array of int to contain numbers of the current row
    int[] array = new int[numberToken.length];

    // map String to int. Beware exception  handling that you should do
    for (int j = 0; j < numberToken.length; j++){
        array[j] = Integer.parseInt(numberToken[j]); 
    }
    // populate current row of the matrix
    matrix[i] = array[j];
}
    Scanner scanner = new Scanner(System.in);
    //number of rows
    int n = Integer.parseInt(scanner.nextLine());
    int[][] matrix = new int[n][];
    for(int i=0;i<n;i++) {
        String line = scanner.nextLine();
        String[] numbers = line.split(" ");
        matrix[i] = new int[numbers.length];
        for(int j=0;j<numbers.length;j++) {
            matrix[i][j] = Integer.parseInt(numbers[j]);
        }
    }