矩阵不向右旋转 90 度

Matrix not rotating 90 degree to Right

我想使用一维数组将矩阵向右旋转 90 度,但它没有给出所需的输出

我以前用过一个简单的方法,它确实有效,所以为了练习,我尝试在一维数组的帮助下进行。

import java.util.Scanner;
public class nine
{
    void main()
    {
        int M, i ,j;
        int z=0;
        int arr[][], x[];

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX");
        M=sc.nextInt();

        arr=new int[M][M];
        x=new int[M*M];
        System.out.println("ENTER THE ELEMENTS IN MATRIX ");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        System.out.println("ORIGINAL MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        
        /* giving matrix x the value of arr from bottom. 
         * example matrix-
            1 2 3 
            4 5 6 
            7 8 9 
            value stored in x- {7 8 9 4 5 6 3 2 1}
        */
        for(i=M-1;i>0;i--)
        {
            for(j=0;j< M;j++)
            {
                x[z]=arr[i][j];
                z++;
            }
        }
        z=0; 
        /* 
         * Adding values of matrix x in matrix arr veritcally. 
         * 
           */
        for(j=0;j< M;j++)
        {
            for(i=0;i< M;i++)
            {
                arr[i][j]= x[z];
                z++;
            }
        }
         /* 
         * Desired output from the example input- 
         *      7 4 1 
                8 5 2 
                9 6 3 
         * 
           */
        System.out.println("ROTATED MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" " );

            }
            System.out.println( );
        }
    }
}


原始数组是双维的,并按照规定的顺序为单维数组赋值。

我想要的输出是:

我得到的输出是:

要旋转,您需要这样的东西:

int arr[][];
int dest[][];

arr = new int[M][M];
dest = new int[M][M];

for (int i=0; i<M, i++) {
  for (int j=0; j<M, j++) {
    dest[M-j-1][i] = arr[i][j];
  }
}

此外,您的倒计时循环需要包括零

for(i=M-1;i>0;i--)

应该是

for(i=M-1;i>=0;i--)

(将 Ricky Mo 的评论变成一个答案,并进行最少的更改和解释)

你们很亲近。无需从根本上改变您的方法。
但是,您获得的输出清楚地表明您的代码没有写入最后一列。
一个明显的结论是您的循环设置不正确。
确实(正如 Ricky Mo 简洁地表述的那样):

change for(i=M-1;i>0;i--) to for(i=M-1;i>=0;i--)

显然它会更频繁地迭代一次。
由于您正确实施的索引魔法,最终出现在右侧(即 0 值列)。

输出为:

ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX
ENTER THE ELEMENTS IN MATRIX 

------------------------------------------------------
ORIGINAL MATRIX 

1 2 3 
4 5 6 
7 8 9 

------------------------------------------------------
ROTATED MATRIX 

7 4 1 
8 5 2 
9 6 3 

例如这里:
https://www.tutorialspoint.com/compile_java_online.php