0

The problem I am having is that I must create program that uses an object to reverse the elements of each row in a 2D Array and prints out the resulting matrix. The code I have only prints out the first 3 numbers of each row in reverse. This is my code:

class Reverse {

    void matrixReverse(int[][] data) {
        int row_val = data.length;
        int col_val = data[0].length;

        int[][] data_reverse = new int[row_val][col_val];

        for(int row = row_val - 1; row >= 0; row--){
            for(int col = col_val - 1; col >= 0; col--){
                data_reverse[row_val - 1 - row][col_val - 1 - col] = data[row][col];
            }
        }
        for(int row = 0; row < row_val; row++){
            for(int col = 0; col < col_val; col++){
                System.out.println(data_reverse[row][col]);
            }
            System.out.println();
        }
    }
}

public class ArrayReverse {

    public static void main(String[] args) {
        int[][] data = {
            {3, 2, 5},
            {1, 4, 4, 8, 13},
            {9, 1, 0, 2},
            {0, 2, 6, 3, -1, -8}
        }; // Creates the array

        Reverse reverse = new Reverse();
        reverse.matrixReverse(data);
    }
}

The output is:

6
2
0

0
1
9

4
4
1

5
2
3

As you can see, it's only printing the first 3 numbers of the rows but I need it to print all of them.

1 Answer 1

2

Your columns are variable width yet you are getting the width from the first row (which may not even exist!). You need to check the size of the array before accessing an element of it. You also have the array size parameters switched, start with row first.

To fix your specific problem, you need to set the col_val for each row inside the row loop.

void matrixReverse(int[][] data) {
    int rowWidth = data.length;
    int[][] reversedData = new int[rowWidth][];
    for (int row = rowWidth - 1; row >= 0; row--) {
        int colWidth = data[row].length;
        int reverseRow = rowWidth - 1 - row;
        reversedData[reverseRow] = new int[colWidth];
        for (int col = colWidth - 1; col >= 0; col--) {
            int reverseCol = colWidth - 1 - col;
            reversedData[reverseRow][reverseCol] = data[row][col];
        }
    }
    for (int row = 0; row < rowWidth; row++) {
        int colWidth = reversedData[row].length;
        for (int col = 0; col < colWidth; col++) {
            System.out.println(reversedData[row][col]);
        }
        System.out.println();
    }
}

P.S. row_val and col_val are bad names. They are widths not values and the naming convention in Java is colWidth and rowWidth not col_width or row_width.

Sign up to request clarification or add additional context in comments.

2 Comments

The line, int[][] reversedData = new int[][rowWidth]; is running into issues because the first pair of square brackets after the new int are empty. I'm very sorry I'm still newer to java and I'm under a lot of stress with school so any help would be appreciated right now.
@JoshC25 my bad got the syntax switched. I haven't had time to test it and it's been ages since I used a 2d array.

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.