2

有人可以告诉我如何将以下内容解析为int[][]类型。这是输入到 java args 中的数字结构"1,2;0,3 3,4;3,4 "(前 4 个数字应该代表一个矩阵以及最后 4 个数字;所以我需要将两者都解析为 a int[][])但是我怎样才能把它解析成一个int[][]类型?

我猜第一件事是:

String[] firstmatrix = args[0].split(";");
String[] rowNumbers = new String[firstmatrix.length];
for(int i=0; i< firstmatrix.length; i++) {   
    rowNumbers = firstmatrix[i].split(",");

 }

但我无法解决-.-

编辑:首先感谢您的所有帮助。但我应该提到不需要异常处理。另外,我只能使用 java.lang 和 java.io

编辑2.0:谢谢大家的帮助!

4

5 回答 5

1

在程序中使用空格的参数将其分成两个不同的参数。对您来说最好的可能是将其更改为以下格式:

1,2;0,3;3,4;3,4

然后

String[] firstMatrix = args[0].split(";");
System.out.print(Arrays.toString(firstMatrix));

生产

[1,2, 0,3, 3,4, 3,4]

并且在做

int[][] ints = new int[firstMatrix.length][2];
int[] intsInside;
for (int i = 0; i < firstMatrix.length; i++) {
    intsInside  = new int[2];
    intsInside[0] = Integer.parseInt(firstMatrix[i].split(",")[0]);
    intsInside[1] = Integer.parseInt(firstMatrix[i].split(",")[1]);
    ints[i] = intsInside;
}

System.out.print("\n" + Arrays.deepToString(ints));

生产

[[1, 2], [0, 3], [3, 4], [3, 4]]

注意:代码中某些位置的值 0、1 和 2 应替换为基于数组长度等的动态值。

于 2020-05-20T10:00:47.043 回答
1

正如您提供的参数一样"1,2;0,3 3,4;3,4",您似乎将 args[0] 和 args[1] 作为输入的两个参数,但您在示例中只显示了 args[0]。以下是您的代码的修改版本,可能会为您提供解决方案的提示

public static void main(String[] args) {
    for (String tmpString : args) {
        String[] firstmatrix = tmpString.split(";");

  // Assuming that only two elements will be there splitter by `,`. 
        // If not the case, you have to add additional logic to dynamically get column length

    String[][] rowNumbers = new String[firstmatrix.length][2];
        for (int i = 0; i < firstmatrix.length; i++) {
            rowNumbers[i] = firstmatrix[i].split(",");
        }
    }
}
于 2020-05-20T10:25:31.020 回答
1
public static void main(String[] args) throws IOException {
        List<Integer[][]> arrays = new ArrayList<Integer[][]>();

        //Considering the k=0 is the show, sum or divide argument
        for(int k=1; k< args.length; k++) {
            String[] values = args[k].split(";|,");
            int x = args[k].split(";").length;
            int y = args[k].split(";")[0].split(",").length;
            Integer[][] array = new Integer[x][y];
            int counter=0;
            for (int i=0; i<x; i++) {
                for (int j=0; j<y; j++) {
                    array[i][j] = Integer.parseInt(values[counter]);
                    counter++;
                }
            }
            //Arrays contains all the 2d array created
            arrays.add(array); 
        }
        //Example to Show the result i.e. arg[0] is show
        if(args[0].equalsIgnoreCase("show"))
            for (Integer[][] integers : arrays) {
                for (int i=0; i<integers.length; i++) {
                    for (int j=0; j<integers[0].length; j++) {
                        System.out.print(integers[i][j]+" ");
                    }
                    System.out.println();
                }
                System.out.println("******");
            }
    }

输入

show 1,2;3,4 5,6;7,8

输出

1 2 
3 4 
******
5 6 
7 8 

带有变量的 inpt 的输入一个 3*3 一个 2*3 矩阵

show 1,23,45;33,5,1;12,33,6 1,4,6;33,77,99

输出

1 23 45 
33 5 1 
12 33 6 
******
1 4 6 
33 77 99 
******
于 2020-05-20T10:23:19.760 回答
0

您可以尝试先用分号分割输入以获得矩阵的行,然后用逗号分割每一行以获得一行的单个值。

以下示例正是这样做的:

public static void main(String[] args) {
    System.out.println("Please enter the matrix values");
    System.out.println("(values of a row delimited by comma, rows delimited by semicolon, example: 1,2;3,4 for a 2x2 matrix):\n");
    // fire up a scanner and read the next line
    try (Scanner sc = new Scanner(System.in)) {
        String line = sc.nextLine();
        // split the input by semicolon first in order to have the rows separated from each other
        String[] rows = line.split(";");
        // split the first row in order to get the amount of values for this row (and assume, the remaining rows have this size, too
        int columnCount = rows[0].split(",").length;
        // create the data structre for the result
        int[][] result = new int[rows.length][columnCount];
        // then go through all the rows
        for (int r = 0; r < rows.length; r++) {
            // split them by comma
            String[] columns = rows[r].split(",");
            // then iterate the column values,
            for (int c = 0; c < columns.length; c++) {
                // parse them to int, remove all whitespaces and put them into the result
                result[r][c] = Integer.parseInt(columns[c].trim());
            }
        }

        // then print the result using a separate static method
        print(result);
    }
}

打印矩阵的方法将int[][]输入作为输入,如下所示:

public static void print(int[][] arr) {
    for (int r = 0; r < arr.length; r++) {
        int[] row = arr[r];
        for (int v = 0; v < row.length; v++) {
            if (v < row.length - 1)
                System.out.print(row[v] + " | ");
            else
                System.out.print(row[v]);
        }
        System.out.println();
        if (r < arr.length - 1)
            System.out.println("—————————————");
    }
}

执行此代码并1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4在输出中输入值

1 | 1 | 1 | 1
—————————————
2 | 2 | 2 | 2
—————————————
3 | 3 | 3 | 3
—————————————
4 | 4 | 4 | 4
于 2020-05-20T10:14:19.627 回答
0

我尝试了 java8 流方式,结果为 int[][]

String s = "1,2;0,3;3,4;3,4";
int[][] arr = Arrays.stream(s.split(";")).
       map(ss -> Stream.of(ss.split(","))
            .mapToInt(Integer::parseInt)
            .toArray())
       .toArray(int[][]::new);

List<List<Integer>>应该是一样的效果。

String s = "1,2;0,3;3,4;3,4";
        List<List<Integer>> arr = Arrays.stream(s.split(";")).
                map(ss -> Stream.of(ss.split(","))
                        .map(Integer::parseInt)
                        .collect(Collectors.toList()))
                .collect(Collectors.toList());
        System.out.println(arr);

希望能帮助到你

于 2020-05-20T10:24:14.933 回答