Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,788 questions

51,694 answers

573 users

How to rotate a matrix 90 degrees clockwise in Java

2 Answers

0 votes
import java.util.Arrays;
 
public class RotateMatrix90 {
    // Function to rotate the matrix 90 degrees clockwise
    static void rotate90Clockwise(int[][] matrix) {
        int rows = matrix.length;
        
        // Step 1: Transpose the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = i; j < rows; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
 
        // Step 2: Reverse each row
        for (int i = 0; i < rows; i++) {
            for (int j = 0, k = rows - 1; j < k; j++, k--) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][k];
                matrix[i][k] = temp;
            }
        }
    }
 
    // Function to print the matrix
    static void printMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            System.out.println(Arrays.toString(row));
        }
    }
 
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
 
        System.out.println("Original Matrix:");
        printMatrix(matrix);
 
        rotate90Clockwise(matrix);
 
        System.out.println("\nRotated Matrix:");
        printMatrix(matrix);
    }
}
 
 
 
/*
run:
 
Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Rotated Matrix:
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]
 
*/

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz
0 votes
import java.util.Arrays;
 
public class RotateMatrix90 {
    // Function to rotate the matrix 90 degrees clockwise
    static int[][] rotate90Clockwise(int[][] matrix) {
        int rows = matrix.length;     // Number of rows
        int cols = matrix[0].length;  // Number of columns 
        
        int[][] rotated = new int[cols][rows];
 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotated[j][rows - 1 - i] = matrix[i][j]; // Mapping to rotated position
            }
        }
        return rotated;
    }
 
    // Function to print the matrix
    static void printMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            System.out.println(Arrays.toString(row));
        }
    }
 
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
 
        System.out.println("Original Matrix:");
        printMatrix(matrix);
 
        int[][] rotated = rotate90Clockwise(matrix);
 
        System.out.println("\nRotated Matrix:");
        printMatrix(rotated);
    }
}
 
 
 
/*
run:
 
Original Matrix:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
 
Rotated Matrix:
[9, 5, 1]
[10, 6, 2]
[11, 7, 3]
[12, 8, 4]
 
*/

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz

Related questions

2 answers 133 views
2 answers 144 views
2 answers 156 views
2 answers 124 views
2 answers 121 views
...