Rotate the matrix 180 degree in Java?


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

As per the problem statement we have to rotate the given matrix to 180 degrees. It means we have to interchange the rows of the given matrix in symmetric- vertically.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Suppose the original matrix is

{
   {10, 20, 30},
   {40, 50, 60},
   {70, 80, 90}
}

After rotating the matrix to 180 degrees:

{
   {90, 80, 70},
   {60, 50, 40},
   {30, 20, 10}
}

Instance-2

Suppose the original matrix is

{
   {1, 2, 3},
   {4, 5, 6},
   {7, 8, 9}
}

After rotating the matrix to 180 degrees:

{
   {9, 8, 7},
   {6, 5, 4},
   {3, 2, 1}
}

Instance-3

Suppose the original matrix is

{
   {11, 22, 33},
   {44, 55, 66},
   {77, 88, 99}
}

After rotating the matrix to 180 degrees:

{
   {99, 88, 77},
   {66, 55, 44},
   {33, 22, 11}
}

Algorithm

Step 1 − Declare and initialize an integer type multi-dimensional array.

Step 2 − Declare two integer type variables to store the length of the rows and columns of a given matrix.

Step 3 − Take a nested for loop to rotate the matrix to 180 degree and store the new matrix into another empty matrix.

Step 4 − Print the resultant matrix as output.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array Elements

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Matrix with pow() Function

In this approach, matrix elements will be initialized in the program. Then as per the algorithm replace the matrix elements by its square. Here we will make use of inbuilt Pow() function to get the square of an element.

Example

public class Main{
   public static void main(String[] args){

      //declare a matrix with some random values
      int[][] inputMatrix = { 
         {10, 20, 30},
         {40, 50, 60},
         {70, 80, 90}
      };

      //declare a variable to store the row count
      int r = inputMatrix.length;

      //declare a variable to store the row count
      int c = inputMatrix[0].length;

      //declare an empty matrix
      int[][] rotatedMAt = new int[r][c];

      //take nested for loop to rotate the matrix to 180 degree
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            rotatedMAt[i][j] = inputMatrix[r - i - 1][c - j - 1];
         }
      }

      //print the given matrix
      System.out.println("Given Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }

      //print the rotated matrix
      System.out.println("Rotated- 180 degree Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(rotatedMAt[i][j] + " ");
         }
         System.out.println();
      }
   }
}

Output

Given Matrix:
10 20 30 
40 50 60 
70 80 90 
Rotated- 180 degree Matrix:
90 80 70 
60 50 40 
30 20 10

Approach-2: By Using User Defined Method

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm rotate the matrix to 180 degrees.

Example

public class Main{

   //user-defined method to rotate the matrix to 180 degree
   public static void Rotate(int[][] inputMatrix){

      //declare a variable to store the row count
      int r = inputMatrix.length;
      
      //declare a variable to store the row count
      int c = inputMatrix[0].length;
      
      //declare an empty matrix
      int[][] rotatedMAt = new int[r][c];
      
      //take nested for loop to rotate the matrix to 180 degree
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            rotatedMAt[i][j] = inputMatrix[r - i - 1][c - j - 1];
         }
      }
      
      //print the given matrix
      System.out.println("Given Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }
      
      //print the rotated matrix
      System.out.println("Rotated- 180 degree Matrix:");
      for (int i = 0; i < r; i++){
         for (int j = 0; j < c; j++){
            System.out.print(rotatedMAt[i][j] + " ");
         }
         System.out.println();
      }
   }
   public static void main(String[] args){
      
      //declare a matrix with some random values
      int[][] inpMatrix = {
         {22, 12, 54},
         {2, 76, 23},
         {124, 67, 34}
      };
      
      //call the user-defined method
      Rotate(inpMatrix);
   }
}

Output

Given Matrix:
22 12 54 
2 76 23 
124 67 34 
Rotated- 180 degree Matrix:
34 67 124 
23 76 2 
54 12 22

In this article, we explored different approaches to rotate the matrix 180 degrees by using Java programming language.

Updated on: 09-Mar-2023

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements