Java 将命令行参数 (args[]) 解析为 int[][] 类型

Java parsing command line arguments ( args[]) into an int[][] Type

有人可以告诉我如何将以下内容解析为 int[][] 类型。这是输入到 java args "1,2;0,3 3,4;3,4 " 中的数字结构(前 4 个数字应该表示矩阵以及最后 4 个数字;所以我需要将两者都解析为 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

edit 2.0: 谢谢大家的帮助!

在程序参数中使用 space 将其拆分为两个不同的参数。最适合您的可能是将其更改为以下格式:

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 应替换为基于数组长度等的动态值。

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

下面的例子就是这样做的:

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
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 

input for inpt with varible 1 3*3 1 2*3 matrix

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 
******

我尝试了 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);

希望对您有所帮助

由于您提供了 "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(",");
        }
    }
}