【问题标题】:Fill rows of 2D array with input from a String用来自字符串的输入填充二维数组的行
【发布时间】:2019-03-08 15:31:50
【问题描述】:

我有以下问题: 给出了我的矩阵每一行的值,列用空格分隔 - 所以我在字符串数组中输入所有行值,删除空格并将数字解析为一个 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()];

【问题讨论】:

  • 您的描述与您的代码不符。

标签: java arrays matrix multidimensional-array


【解决方案1】:

你应该 split() 输入一些字符的字符串(在你的例子中是空格)。

示例如何将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]);
}

【讨论】:

    【解决方案2】:

    这里有重要的问题:

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

    【讨论】:

    • 感谢您的建议! int 矩阵的部分 [][] = null;很有帮助,我不知道这是可能的:)
    • 你还在外面。男孩,这是一个漫长的假期。我希望你做得很好......无关:如果你有一两分钟,请看看stackoverflow.com/a/55650516/1531124,让我知道你对这个问题给出的两个答案的看法。
    • @GhostCat 你好。是的,很好,你好吗?确实,这是一个很长的假期。下个月我可能也不会出现在 SO 上。因此,如果您想问我任何问题,请在 gmail.com 上获取我的电子邮件 ebundy。关于它,您的请求是否始终有效?
    • 很高兴收到您的来信。你收到一封信件。我希望这有效。我已经收到了有关该答案的反馈,但请随时发表评论。当其他人声称他们更了解时,我总是试图理解。
    • 我明白了,我会回答的。顺便感谢您的其他评论:)
    【解决方案3】:

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

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

    【讨论】:

    • 是的,这正是我想要做的,但我仍在努力解析动态输入的元素。您的代码帮助很大,我现在更好地理解了事情。谢谢! :)
    猜你喜欢
    • 2015-05-22
    • 2021-02-10
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多