简体   繁体   English

使用嵌套的 for 循环修改二维数组

[英]Modifying a 2D array using a nested for loop

I am trying to print out the 'middle' of the 2D array (a).我正在尝试打印出二维数组 (a) 的“中间”。 For example, for given arrays in my code, I would like to print:例如,对于我的代码中给定的 arrays,我想打印:

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

However I was only able to print out the 'middle' values.但是我只能打印出“中间”值。 I would like to modify the 2D array (a) in the method inner and print it in main instead, and not use System.out.println in the nested for loop.我想修改方法 inner 中的二维数组 (a)并将其打印在 main 中,而不是在嵌套的 for 循环中使用 System.out.println 。 How would I go about doing this?我将如何 go 这样做?

Here is my code:这是我的代码:

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

Create a new array outside the loop and then fill that array inside the loop by translating the indices between the two arrays:在循环外创建一个新数组,然后通过转换两个 arrays 之间的索引来填充循环内的数组:

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

If you just want to print the middle values (my definition for this code example is: middle = full array minus first and last element ), you can make use of a StringBuilder :如果您只想打印中间值(我对此代码示例的定义是: 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());
    }
}

The output will be output 将是

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

You can use Arrays.stream(T[],int,int) method to iterate over a given range of an array:您可以使用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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM