public class Transform {
    public int[][] transformImage(int[][] mat, int n) {
        int[][] result = mat;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                result[i][j] = mat[n-j-1][i];
            }
        }
        return result;
    }
    public static void main(String[] args){
        int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
        //3是mat的矩阵mat的阶数
        int[][] result = new Transform().transformImage(mat,3);
        for(int i = 0;i < 3;i++){
            for(int j = 0;j < 3;j++){
                System.out.print (result[i][j]+" ");
            }
            System.out.println();
        }
    }
}/***为什么输出不是
7 4 1
8 5 2
9 6 3
竟然是这样的结果:
7 4 7 
8 5 4 
9 4 7 
*****/

解决方案 »

  1.   

    值传递与引用传递。注意看int[][] result = mat; 改动result其实也是改动了mat。
      

  2.   


     for(int i = 0;i < n;i++){
               for(int j = 0;j < n/2;j++){
                    result[i][j] = mat[n-j-1][i];
                }
    }
      

  3.   

    result[i][j] = mat[n-j-1][i];你这样直接赋值会覆盖掉原位置上的数字.
      

  4.   

    int[][] result = new int[3][3];
    值传递与引用传递
    get it
      

  5.   


    public class test {
        public int[][] transformImage(int[][] mat, int n) {
            int[][] result = new int[n][n];
            
            
            for(int i = 0;i < n;i++){
                for(int j = 0;j < n;j++){
                    result[i][j] = mat[n-j-1][i];
                }
            }
            return result;
        }
        public static void main(String[] args){
            int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
            //3是mat的矩阵mat的阶数
            int[][] result = new test().transformImage(mat,3);
            for(int i = 0;i < 3;i++){
                for(int j = 0;j < 3;j++){
                    System.out.print (result[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
      

  6.   

     int[][] result = mat;
    这只是传了一个引用,后面对result的修改也是对mat的修改。
    需要为result new一个二维数组,分配存储空间。  
       public int[][] transformImage(int[][] mat, int n) {
            int[][] result=new int[3][3];
            for(int i = 0;i < n;++i){
                for(int j = 0;j < n;++j){
                    result[i][j] = mat[j][i];
                }
            } 
            return result;
        }
        
      

  7.   

    int[][] result = mat;result与mat产生了别名问题
      

  8.   

     int[][] result = mat;
            for(int i = 0;i < n;i++){
                for(int j = 0;j < n;j++){
                    result[i][j] = mat[n-j-1][i];
                }
            }
    --------------------------------------------------------
     int[][] result  = new int[mat.length][];