用来自字符串的输入填充二维数组的行

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


慕运维8079593
浏览 105回答 3
3回答

holdtom

您应该split()通过一些字符输入字符串(在您的示例中为空格)。示例如何转换String为数组String(使用split()方法)// Example inputString input&nbsp; = "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++) {&nbsp; &nbsp; // Get element from "position"&nbsp; &nbsp; System.out.println(numbers[position]);}示例如何转换String为数组int// Example inputString 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++) {&nbsp; &nbsp; number[position] = Integer.parseInt(strings[position]);}

侃侃无极

很难说,但据我了解,您正试图通过扫描仪逐行输入矩阵。这可以解决你的问题。&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; //number of rows&nbsp; &nbsp; int n = Integer.parseInt(scanner.nextLine());&nbsp; &nbsp; int[][] matrix = new int[n][];&nbsp; &nbsp; for(int i=0;i<n;i++) {&nbsp; &nbsp; &nbsp; &nbsp; String line = scanner.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; String[] numbers = line.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; matrix[i] = new int[numbers.length];&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<numbers.length;j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[i][j] = Integer.parseInt(numbers[j]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

长风秋雁

在这里你有重要的问题:for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; arr [i] = input.nextLine().replaceAll("\\s+",""); // loses the separator between the number&nbsp; &nbsp; 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&nbsp;&nbsp;int matrix [][] = null; // init it later : as you would have the two dimensions knowledgefor (int i = 0; i < n; i++) {&nbsp; &nbsp; String[] numberToken = input.nextLine().split("\\s");&nbsp;&nbsp; &nbsp; // matrix init : one time&nbsp; &nbsp; if (matrix == null){ matrix [][] = new int[n][numberToken.length]; }&nbsp; &nbsp; // array of int to contain numbers of the current row&nbsp; &nbsp; int[] array = new int[numberToken.length];&nbsp; &nbsp; // map String to int. Beware exception&nbsp; handling that you should do&nbsp; &nbsp; for (int j = 0; j < numberToken.length; j++){&nbsp; &nbsp; &nbsp; &nbsp; array[j] = Integer.parseInt(numberToken[j]);&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // populate current row of the matrix&nbsp; &nbsp; matrix[i] = array[j];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java