【问题标题】:Modifying a 2D array using a nested for loop使用嵌套的 for 循环修改二维数组
【发布时间】:2019-11-20 09:24:29
【问题描述】:

我正在尝试打印二维数组 (a) 的“中间”。例如,对于我的代码中的给定数组,我想打印:

[3,4,5,6]
[4,5,6,7]

但是我只能打印出“中间”值。我想在方法 inner 中修改 2D 数组 (a) 并将其打印在 main 中,而不是在嵌套的 for 循环中使用 System.out.println。我该怎么做呢?

这是我的代码:

public static int[][] inner(int[][] a) {
    int rowL = a.length - 1;
    int colL = a[1].length - 1;

    for (int row = 1; row < rowL; row++) {
        for (int col = 1; col < colL; col++) {
            //System.out.print(a[row][col]);
            a = new int[row][col];
        }
        System.out.println();
    }
    return a;
}
public static void main(String[] args) {
    int[][] a = {
            {1, 2, 3, 4, 5, 6},
            {2, 3, 4, 5, 6, 7},
            {3, 4, 5, 6, 7, 8},
            {4, 5, 6, 7, 8, 9}};

    for (int[] row : a) {
        System.out.println(Arrays.toString(row));
    }

    System.out.println();

    for (int[] row : inner(a)) {
        System.out.println(Arrays.toString(row));
    }
}

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    在循环外创建一个新数组,然后通过转换两个数组之间的索引来填充循环内的数组:

    public static int[][] inner (int[][] a) {
        int rowL = a.length - 1;
        int colL = a[1].length -1;
        int[][] ret = new int[rowL - 1][colL - 1];
    
        for (int row = 1; row < rowL; row++) {
            for (int col = 1; col < colL ; col++) {
                ret[row - 1][col - 1] = a[row][col];
            }
        }
    
        return ret;
    }
    

    【讨论】:

    • 看不懂你怎么return ret;
    • 我明白了!谢谢!
    【解决方案2】:

    如果你只想打印 middle 值(我对这个代码示例的定义是:middle = full array minus first and last element),你可以使用StringBuilder:

    public static void main(String[] args) {
        int[][] a = {
                        { 1, 2, 3, 4, 5, 6 },
                        { 2, 3, 4, 5, 6, 7 },
                        { 3, 4, 5, 6, 7, 8 },
                        { 4, 5, 6, 7, 8, 9 }
                    };
    
        for (int[] b : a) {
            // create a String output for each inner array
            StringBuilder outputBuilder = new StringBuilder();
            // append an introducing bracket
            outputBuilder.append("[");
            // make the values to be printed ignore the first and last element
            for (int i = 1; i < b.length - 1; i++) {
                if (i < b.length - 2) {
                    /*
                     * append a comma plus whitespace 
                     * if the element is not the last one to be printed
                     */
                    outputBuilder.append(b[i]).append(", ");
                } else {
                    // just append the last one without trailing comma plus whitespace 
                    outputBuilder.append(b[i]);
                }
            }
            // append a closing bracket
            outputBuilder.append("]");
            // print the result
            System.out.println(outputBuilder.toString());
        }
    }
    

    输出将是

    [2, 3, 4, 5]
    [3, 4, 5, 6]
    [4, 5, 6, 7]
    [5, 6, 7, 8]
    

    【讨论】:

    • 我希望进行修改,以便最后我打印一个二维数组而不是字符串、整数等。
    【解决方案3】:

    您可以使用Arrays.stream(T[],int,int) 方法遍历数组的给定范围:

    int[][] arr = {
            {1, 2, 3, 4, 5, 6},
            {2, 3, 4, 5, 6, 7},
            {3, 4, 5, 6, 7, 8},
            {4, 5, 6, 7, 8, 9}};
    
    int[][] middle = Arrays.stream(arr, 1, arr.length - 1)
            .map(row -> Arrays.stream(row, 1, row.length - 1)
                    .toArray())
            .toArray(int[][]::new);
    
    // output
    Arrays.stream(middle).map(Arrays::toString).forEach(System.out::println);
    
    [3, 4, 5, 6]
    [4, 5, 6, 7]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多