
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- JavaScript Program for Rotate a Matrix by 180 degrees
- Rotate a matrix by 90 degree without using any extra space in C++
- Java Program to Rotate Matrix Elements
- Rotate a matrix by 90 degree in clockwise direction without using any extra space in C++
- How to rotate a matrix of size n*n to 90 degree using C#?
- Rotate Matrix in Python
- Java Program for Rotate the Matrix Right by K times
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Golang Program To Rotate Matrix Elements
- Write a program in Java to rotate a matrix by 90 degrees in anticlockwise direction
- Generate a Vandermonde matrix of given degree in Python
- Rotate div with Matrix transforms using CSS
- Rotate a List in Java
- Generate a Pseudo-Vandermonde matrix of given degree in Python
- JavaScript Program for Rotate the matrix right by K times
