2

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

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I do not understand how can you return ret;
I got it! Thank you!
0

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]

1 Comment

I wish to make the modification such that at the end I am printing a 2D array not a string, ints etc.
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.