简体   繁体   中英

Modifying a 2D array using a nested for loop

I am trying to print out the 'middle' of the 2D array (a). For example, for given arrays in my code, I would like to print:

[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. How would I go about doing this?

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:

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 :

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

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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