有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java用中间线交换2D矩阵对角线

我有一个Java 2D数组

String arraylist numberSeq[][];

在数组列表中,有一个从1到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

所以这个数字会是

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

当我只能声明一个局部变量时,如何实现这一点

如果一个局部变量无法实现,那么我需要的最小局部变量数是多少


共 (3) 个答案

  1. # 1 楼答案

    以下循环将按照您的问题进行对角线交换:

        int n=5;
        int[][] newmat = new int[5][5];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                newmat[(n-1) - j][(n-1)- i] = matrix[i][j];
            }
        }
    
  2. # 2 楼答案

    这应该会有帮助。是的,用一个局部变量交换是可能的

    public swapDiagonally(int[][] mtx) {
      for(int i = 0 ;i< mtx.length; i++){
         for(int j = 0; j < mtx[0].length - i; j++){ 
            int temp = mtx[i][j];
            mtx[j][i]; = mtx[mtx.length-1-i][mtx[0].length-1-j];
            mtx[mtx.length-1-i][mtx[0].length-1-j] = temp;
         }
      } 
    }
    

    我基本上是每N列遍历“总计行-N”,因此,帮助我遍历这个结构:

     1 2 3 4
     1 2 3
     1 2
     1
    

    对于4x4阵列

  3. # 3 楼答案

    你可以通过制作另一个teo维数组来实现这一点,以正确的顺序迭代第一个数组,并将其发布到一个新的数组中

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

    我希望这就是你所说的“1个局部变量”的意思

    输出为:

    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