Java Program to Clockwise Rotate the Matrix Elements

In the previous article, we have discussed Java Program to Find the Frequency of Odd and Given Numbers in the Given Matrix

In this article we are going to see how we can write a program to clockwise rotate the elements in a matrix in JAVA language.

Java Program to Clockwise Rotate the Matrix Elements

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to to Clockwise Rotate the Matrix Elements.

Method-1: Java Program to Clockwise Rotate the Matrix Elements By Static Initialization of Array Elements

Approach:

  • Initialize and an array of size 3×3, with elements.
  • Use a while loop to iterate the row and column index. Inside for loops store the elements of the next array to replaced in variable prev and the replacing element in variable curr and switch the elements.
  • Print the matrix.

Program:

public class matrix{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,32},{40,54,62},{70,20,60}};
        int row, col ;

        System.out.println("\nThe matrix elements are:");
        printMatrix(arr);

        rotateArr(arr);
    }
    //Function that clockwise rotates the matrix
    static void rotateArr(int arr[][])
    {
        int rowSize = 3, colSize = 3, row = 0, col = 0,prev, curr;

        while(row<rowSize&&col<colSize)
        {
            if(row+1==rowSize||col+1==colSize)
                break;
            // Storing the elements of the next row which
            //  will replace the element of the current row
            prev = arr[row+1][col];
            // Moves the elements of the first row
            for(int i = col; i<colSize;i++)
            {
                curr = arr[row][i];
                arr[row][i] = prev;
                prev = curr;
            }
            row++;

            // Move elements of last columns
            for(int i = row; i<rowSize;i++)
            {
                curr = arr[i][rowSize-1];
                arr[i][rowSize-1] = prev;
                prev = curr;
            }
            colSize--;
            // Move elements of last row
            if(row<rowSize)
            {
                for(int i = colSize-1; i>= col; i--)
                {
                    curr = arr[rowSize-1][i];
                    arr[rowSize-1][i] = prev;
                    prev = curr;
                }
            }
            rowSize--;
            // Move elements of first column
            if(col<colSize)
            {
                for(int i = rowSize-1; i>= row; i--)
                {
                    curr = arr[i][col];
                    arr[i][col] = prev;
                    prev = curr;
                }
            }
            col++;

            System.out.println("\nThe matrix after rotating-");
            printMatrix(arr);
        }
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}
Output:

The matrix elements are:

19 25 32 
40 54 62 
70 20 60 
The matrix after rotating-

40 19 25 
70 54 32 
20 60 62

Method-2: Java Program to Clockwise Rotate the Matrix Elements By Dynamic Initialization of Array Elements

Approach:

  • Declare one array of size 3×3.
  • Ask the user for input of array elements and store them in the array using two for loops.
  • Use a while loop to iterate the row and column index. Inside for loops store the elements of the next array to replaced in variable prev and the replacing element in variable curr and switch the elements.
  • Print the matrix.

Program:

import java.util.Scanner;
public class matrix
{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3];
        int row, col ;

        // Taking matrix1 input
        System.out.println("\nEnter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();

        System.out.println("\nThe matrix elements are : ");
        printMatrix(arr);

        rotateArr(arr);
    }
    //Function that clockwise rotates the matrix
    static void rotateArr(int arr[][])
    {
        int rowSize = 3, colSize = 3, row = 0, col = 0,prev, curr;

        while(row<rowSize&&col<colSize)
        {
            if(row+1==rowSize||col+1==colSize)
                break;
            // Storing the elements of the next row which
            //  will replace the element of the current row
            prev = arr[row+1][col];
            // Moves the elements of the first row
            for(int i = col; i<colSize;i++)
            {
                curr = arr[row][i];
                arr[row][i] = prev;
                prev = curr;
            }
            row++;

            // Move elements of last columns
            for(int i = row; i<rowSize;i++)
            {
                curr = arr[i][rowSize-1];
                arr[i][rowSize-1] = prev;
                prev = curr;
            }
            colSize--;
            // Move elements of last row
            if(row<rowSize)
            {
                for(int i = colSize-1; i>= col; i--)
                {
                    curr = arr[rowSize-1][i];
                    arr[rowSize-1][i] = prev;
                    prev = curr;
                }
            }
            rowSize--;
            // Move elements of first column
            if(col<colSize)
            {
                for(int i = rowSize-1; i>= row; i--)
                {
                    curr = arr[i][col];
                    arr[i][col] = prev;
                    prev = curr;
                }
            }
            col++;

            System.out.println("\nThe matrix after rotating : ");
            printMatrix(arr);
        }
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}


Output:

Enter matrix elements : 9 8 3 4 5 6 7 3 8

The matrix elements are :

9 8 3 
4 5 6 
7 3 8 
The matrix after rotating :

4 9 8 
7 5 3 
3 8 6

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: