用来自字符串的输入填充2D数组的行

时间:2019-03-08 15:31:51

标签: java arrays matrix multidimensional-array

我有以下问题: 给定矩阵的每一行的值,各列之间用空格隔开-因此,我在String数组中输入所有行值,删除空格并将数字解析为int数组。现在,每行的值看起来像1个数字“ 12345”,而应该是“ 1 2 3 4 5”。

如何首先分隔数字,然后通过将元素添加到每一行来填充矩阵?谢谢! 这是我的代码:

    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()];

3 个答案:

答案 0 :(得分:1)

您应该split()用一些字符(示例中的空格)输入String。

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

// Example input
String input  = "1 2 3 4 5";

// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] numbers = input.split(" ");

for (int position = 0; position < numbers.length; position++) {
    // Get element from "position"
    System.out.println(numbers[position]);
}

示例如何将String转换为int的数组

// Example input
String input = "1 2 3 4 5";

// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] strings = input.split(" ");

// Create new array for "ints" (with same size!)
int[] number = new int[strings.length];

// Convert all of the "Strings" to "ints"
for (int position = 0; position < strings.length; position++) {
    number[position] = Integer.parseInt(strings[position]);
}

答案 1 :(得分:1)

这里有重要问题:

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
}

如果在提交的每一行中填充矩阵,则可以使用更少的变量来进行处理。
没有经过测试的代码,但是您应该有一个主意。

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];
}

答案 2 :(得分:1)

很难说,但是据我了解,您正在尝试通过扫描仪逐行输入矩阵。 这样可以解决您的问题。

    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]);
        }
    }