Transpose of a 2D Matrix Program in Java (ICSE BlueJ)

⚡ Direct Answer & Key Takeaway

The transpose of an `M x N` matrix `A` is a new `N x M` matrix `T` obtained by turning every row into a column using the assignment `T[j][i] = A[i][j]`. Because dimensions swap when `M != N`, always declare `int[][] T = new int[c][r];` rather than `[r][c]`. If the matrix is strictly square (`M == N`) and space is constrained, you can swap `A[i][j]` with `A[j][i]` in place across the upper triangle where `j > i`.

1. Swapping Dimensions for Rectangular vs. Square Matrices

Many students treat transpose as merely printing `A[j][i]` without storing it, or allocating a second array with identical `new int[r][c]` dimensions. Both habits lose marks in ICSE Section B when a board question specifies a `3 x 2` or `2 x 4` array and requires constructing the transposed matrix in memory. A `2 x 3` original matrix becomes a `3 x 2` transposed matrix: two rows turn into two columns, and three columns become three rows. Declaring `trans = new int[c][r]` maps `trans[j][i] = mat[i][j]` cleanly without overflow.

  • Original `mat` has `r` rows and `c` columns (`mat[r][c]`).
  • Transposed `trans` must have `c` rows and `r` columns (`trans[c][r]`).
  • Display `trans` using outer loop `i < c` and inner loop `j < r`.
  • Diagonal elements `mat[i][i]` never change position during transposition.

2. Step-by-Step Dry Run on a 2x3 Matrix

Let `mat` be a `2 x 3` matrix with row 0 `{10, 20, 30}` and row 1 `{40, 50, 60}`. When `i = 0`, `j` iterates `0, 1, 2`, copying `mat[0][0] = 10` to `trans[0][0]`, `mat[0][1] = 20` to `trans[1][0]`, and `mat[0][2] = 30` to `trans[2][0]`. Next, when `i = 1`, `mat[1][0] = 40` moves to `trans[0][1]`, `mat[1][1] = 50` moves to `trans[1][1]`, and `mat[1][2] = 60` moves to `trans[2][1]`. Printing `trans` (`3 x 2`) outputs rows `{10, 40}`, `{20, 50}`, and `{30, 60}`.

  • Row 0 `{10, 20, 30}` becomes Column 0 of `trans`.
  • Row 1 `{40, 50, 60}` becomes Column 1 of `trans`.
  • Notice `trans.length` equals `3` while `trans[0].length` equals `2`.

3. In-Place Transpose for Square Matrices (Bonus Viva Logic)

During ICSE practical viva or Section A output prediction, examiners often test whether you can transpose an `N x N` square array in place without creating `trans[][]`. If you run `j` from `0` to `N - 1` and swap `mat[i][j]` with `mat[j][i]`, every pair gets swapped twice and the matrix reverts to its original form! The fix is starting the inner loop at `j = i + 1` so only elements strictly above the main diagonal swap with their mirror partners below it.

  • Upper-triangle loop: `for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++)`.
  • Three-line swap uses `int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp;`.
  • In-place transposition fails for rectangular matrices (`r != c`) because row/column sizes differ.

4. MatrixTranspose.java (ICSE BlueJ)

import java.util.*;

public class MatrixTranspose {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows and columns: ");
        int r = sc.nextInt();
        int c = sc.nextInt();

        int[][] mat = new int[r][c];
        int[][] trans = new int[c][r];

        System.out.println("Enter " + (r * c) + " elements:");
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                mat[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                trans[j][i] = mat[i][j];
            }
        }

        System.out.println("\nOriginal Matrix (" + r + "x" + c + "):");
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                System.out.print(mat[i][j] + "\t");
            }
            System.out.println();
        }

        System.out.println("\nTransposed Matrix (" + c + "x" + r + "):");
        for (int i = 0; i < c; i++) {
            for (int j = 0; j < r; j++) {
                System.out.print(trans[i][j] + "\t");
            }
            System.out.println();
        }
        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter rows and columns: 2 3
Enter 6 elements:
10 20 30 40 50 60

Original Matrix (2x3):
10	20	30	
40	50	60	

Transposed Matrix (3x2):
10	40	
20	50	
30	60

5. CISCE Variable Description Table (VDT) & Dry Run

VariableData TypeExam Purpose & Sample Value
scScannerReads matrix dimensions and element inputs
r, cintRow and column counts of original matrix (r = 2, c = 3)
mat[][]int[][]Original 2x3 array storing input values
trans[][]int[][]Transposed 3x2 array storing mat[i][j] at trans[j][i]
i, jintNested loop indices used to copy and display both matrices
🎯 1-on-1 Live Mentorship & Free Study Kit

Fix Matrix Indexing Bugs with Live ICSE Java Practice

Book a 1-on-1 BlueJ session to master rectangular transposes, upper-triangle swaps, and full-mark VDT writing.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Allocating `new int[r][c]` for `trans` instead of `new int[c][r]`, which throws `ArrayIndexOutOfBoundsException` on any non-square matrix.
  • Avoid Mistake: Swapping `mat[i][j]` and `mat[j][i]` across the entire `0..n-1` grid during an in-place transpose, accidentally restoring the original matrix.
  • Avoid Mistake: Printing `trans` using `i < r` and `j < c` instead of `i < c` and `j < r`, truncating or crashing the output loop.
  • Examiner / Mentor Tip: If a Section B question specifies a fixed size like `4 x 4`, declare `final int N = 4;` or hardcode `4` cleanly as directed, and still construct the transposed array unless asked only to print.
  • Examiner / Mentor Tip: Write a clear comment explaining why `trans` uses `[c][r]` dimensions—examiners appreciate explicit clarity on rectangular arrays.
  • Examiner / Mentor Tip: Know both approaches: auxiliary `trans[c][r]` works for all matrices, while `j = i + 1` in-place swap works only for square matrices.

7. Interactive Concept Check Quiz

Question: A student runs an in-place transpose on a `3 x 3` array `a` using `for(i=0; i<3; i++) for(j=0; j<3; j++) { swap a[i][j], a[j][i]; }`. What will `a` contain after the loops finish?

8. Frequently Asked Questions

Do I need a second array to find the transpose of a matrix in Java?
For rectangular matrices where rows and columns differ (such as `2 x 3` becoming `3 x 2`), yes—you need a second array `trans[c][r]` or you can only print transposed values without storing them. For square matrices (`N x N`), you can transpose in place by swapping only when `j > i`.
What is the difference between printing mat[j][i] and storing trans[j][i] = mat[i][j]?
Printing `mat[j][i]` with `j` in the outer loop displays the transposed layout on screen, but leaves the data untouched in memory. Storing `trans[j][i] = mat[i][j]` builds an actual transposed matrix object you can pass to further operations like symmetry checks or matrix multiplication.
What happens if you transpose a matrix twice in a row?
Transposing a matrix twice always returns the exact original matrix, written mathematically as `(A^T)^T = A`. The dimensions flip from `r x c` to `c x r` on the first transpose, and flip straight back to `r x c` with identical elements on the second. This property makes an easy sanity test in BlueJ.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class