Search code examples
javaarraysmultidimensional-arrayswap

Swapping 2D matrix diagonal with a middle line


I have a Java 2D array

String arraylist numberSeq[][];

inside the array list, there is number from 1 to 25,

numberSeq[0][0] = 1, [0][1] = 2, [0][2] = 3 , [0][3] = 4 , [0][4] = 5
numberSeq[1][0] = 6, [1][1] = 7, [1][2] = 8 , [1][3] = 9 , [1][4] = 10
......
numberSeq[4][0] = 21,[4][1] = 22,[4][2] = 23, [4][3] = 24, [4][4] = 25

So the number will be like

1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

After doing a diagonals swap, I wish the output to be like

25 20 15 10 5
24 19 14 9  4
23 18 13 8  3
22 17 12 7  2
21 16 11 6  1

How can I achieve that when I can only declare one local variable?

If one local variable is not achievable, how much is the minimum number of local variables I need?


Solution

  • You can do this by making another teo dimensional array, iterating through your first in the right order and post it to a new one.

    public class MyClass {
        public static void main(String args[]) {
    
            int dimension = 5;
    
            int[][] numberSeq = new int[dimension][dimension];
    
            numberSeq[0][0] = 1; numberSeq[0][1] = 2;numberSeq[0][2] = 3;numberSeq[0][3] = 4;numberSeq[0][4] = 5;
            numberSeq[1][0] = 6; numberSeq[1][1] = 7;numberSeq[1][2] = 8;numberSeq[1][3] = 9;numberSeq[1][4] = 10;
            numberSeq[2][0] = 11;numberSeq[2][1] = 12;numberSeq[2][2] = 13;numberSeq[2][3] = 14;numberSeq[2][4] = 15;
            numberSeq[3][0] = 16;numberSeq[3][1] = 17;numberSeq[3][2] = 18;numberSeq[3][3] = 19;numberSeq[3][4] = 20;
            numberSeq[4][0] = 21;numberSeq[4][1] = 22;numberSeq[4][2] = 23;numberSeq[4][3] = 24;numberSeq[4][4] = 25;
    
            int[][] flippedSeq = new int[dimension][dimension];
    
            // This is the flipping part
            for(int i = 0; i < dimension; i++)
            {
                for(int j = 0; j < dimension; j++)
                {
                    flippedSeq[i][j] = numberSeq[dimension-1-j][dimension-1-i];
                }
            }
    
            PrintMatrix(numberSeq);
            System.out.println();
            PrintMatrix(flippedSeq);
    
    
        }
    
        public static void PrintMatrix(int[][] mat)
        {
            for(int i = 0; i < mat.length; i++)
            {
                for(int j = 0; j < mat[i].length; j++)
                    System.out.print(mat[i][j] + " ");
                System.out.println();
            }
        }
    }
    

    I hope thats what you meant by "1 local variable".

    Output is:

    1 2 3 4 5 
    6 7 8 9 10 
    11 12 13 14 15 
    16 17 18 19 20 
    21 22 23 24 25 
    
    25 20 15 10 5 
    24 19 14 9 4 
    23 18 13 8 3 
    22 17 12 7 2 
    21 16 11 6 1