Transpose a Matrix in Java
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.
Java Program
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
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.
- 1The original is 2 × 3, so
transposedis allocated asnew int[cols][rows], which is 3 × 2. - 2Nested loops visit every cell
matrix[i][j]of the original. - 3
transposed[j][i] = matrix[i][j]writes the value to the mirrored position. - 4Printing
transposedrow by row shows the original columns laid out as rows.
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.
Why: Every cell is copied once into a separate c × r matrix.
Key Concepts
Approach 2: In-Place (Square Matrix)
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
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.
- 1The outer loop walks the rows with
i. - 2The inner loop starts at
j = i + 1, covering only the cells above the main diagonal. - 3Each
matrix[i][j]is swapped withmatrix[j][i]through a temp variable. - 4Diagonal cells, where
i == j, already sit in their transposed position and are never touched.
[[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.
Why: About n²/2 swaps cover the upper triangle, and no second matrix is needed.
Key Concepts
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.