Java Programs›Arrays›Transpose a Matrix

Transpose a Matrix in Java

beginner·  Arrays  ·  Matrix

Problem

The transpose of a matrix swaps its rows and columns: the value at row i, column j moves to row j, column i, so an r × c matrix becomes c × r.

Given a matrix of any shape, build its transpose.

Input
[[5, 8, 2], [1, 6, 9]]
Output
[[5, 1], [8, 6], [2, 9]]

Java Program

Java
import java.util.Arrays; public class TransposeMatrix { public static void main(String[] args) { int[][] matrix = {{5, 8, 2}, {1, 6, 9}}; // 2 x 3 int rows = matrix.length; int cols = matrix[0].length; int[][] transposed = new int[cols][rows]; // dimensions swap: 3 x 2 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transposed[j][i] = matrix[i][j]; // row i becomes column i } } for (int[] row : transposed) { System.out.println(Arrays.toString(row)); } } }

Output

[5, 1] [8, 6] [2, 9]

Core Logic

Transposing is a change of address, not of values — each value is copied to the cell whose row and column indexes are swapped.

How It Works
  1. 1The original is 2 × 3, so transposed is allocated as new int[cols][rows], which is 3 × 2.
  2. 2Nested loops visit every cell matrix[i][j] of the original.
  3. 3transposed[j][i] = matrix[i][j] writes the value to the mirrored position.
  4. 4Printing transposed row by row shows the original columns laid out as rows.
The first column of the original is 5, 1, and after the copy it appears as the first row [5, 1].
💡

Key Point: Allocating the result with swapped dimensions is what makes this work for non-square matrices — new int[rows][cols] would fail as soon as j passed the last row index.

Complexity
Time Complexity: O(r × c)Space Complexity: O(r × c)

Why: Every cell is copied once into a separate c × r matrix.

Key Concepts

2D arraynested for loopindex swapping

Approach 2: In-Place (Square Matrix)

Java
import java.util.Arrays; public class TransposeMatrixInPlace { public static void main(String[] args) { int[][] matrix = {{3, 8, 1}, {6, 2, 7}, {9, 4, 5}}; // must be square int n = matrix.length; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // upper triangle only, so each pair swaps exactly once int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (int[] row : matrix) { System.out.println(Arrays.toString(row)); } } }

Output

[3, 6, 9] [8, 2, 4] [1, 7, 5]

Core Logic

When the matrix is square, rows and columns have the same length, so it can be transposed inside itself by swapping each pair of cells mirrored across the diagonal.

How It Works
  1. 1The outer loop walks the rows with i.
  2. 2The inner loop starts at j = i + 1, covering only the cells above the main diagonal.
  3. 3Each matrix[i][j] is swapped with matrix[j][i] through a temp variable.
  4. 4Diagonal cells, where i == j, already sit in their transposed position and are never touched.
For [[3, 8, 1], [6, 2, 7], [9, 4, 5]], the swaps are 8↔6, 1↔9 and 7↔4, giving [[3, 6, 9], [8, 2, 4], [1, 7, 5]].
💡

Key Point: Starting the inner loop at i + 1 is essential — looping over every j would swap each pair twice and put the matrix right back where it started.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: About n²/2 swaps cover the upper triangle, and no second matrix is needed.

Key Concepts

in-place swapupper trianglesquare matrix

Interview Questions

Why can only square matrices be transposed in place?

A non-square matrix changes shape (r × c becomes c × r), so the result doesn't fit in the original array.

What's the transpose of a transpose?

The original matrix — flipping over the diagonal twice puts every value back.

Learn the Concept

Related Programs