0

I am trying to switch the rows and columns for a 2d array. For example:

[1,1,1]
[2,2,2]
[3,3,3]

goes to

[1,2,3]
[1,2,3]
[1,2,3]

However, my code returns the same 2d array as when it starts. Anyone know how to fix this?

Code:

public static int[][] invert(int[][] array) {
    for(int j = 0; j < array.length; j++) {
        for(int i = 0; i < array[j].length / 2; i++) {
            int temp = array[j][i];
            array[j][i] = array[j][array[j].length - i - 1];
            array[j][array[j].length - i - 1] = temp;
        }
    }
    return array;
}
Abra
  • 19,142
  • 7
  • 29
  • 41
George
  • 9
  • 1
  • 1
    It looks like you're transposing the matrix twice. You switch every position with its transpose position and then switch it back. Double check your loop bounds. – Silvio Mayolo May 05 '21 at 01:37
  • Does this help? https://stackoverflow.com/questions/8422374/multi-dimensional-array-transposing – Abra May 05 '21 at 01:41

0 Answers0