Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Transposing Rows and Columns in Two-Dimensional Arrays

Tech May 15 1

Implementing a Java program to transpose rows and columns in a two-dimnesional array:

Original matrix: 1 2 3 4 5 6 7 8 9

Transposed matrix: 1 4 7 2 5 8 3 6 9

public class MatrixTransposer {
    public static void main(String[] args) {
        int[][] matrixData = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        
        System.out.println("Original matrix:");
        displayMatrix(matrixData);
        
        System.out.println("\nTransposed matrix:");
        transposeAndDisplay(matrixData);
    }
    
    private static void displayMatrix(int[][] matrix) {
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                System.out.print("\t" + matrix[row][col]);
            }
            System.out.println();
        }
    }
    
    private static void transposeAndDisplay(int[][] matrix) {
        for (int col = 0; col < matrix[0].length; col++) {
            for (int row = 0; row < matrix.length; row++) {
                System.out.print("\t" + matrix[row][col]);
            }
            System.out.println();
        }
    }
}

Output:

Original matrix:
	1	2	3
	4	5	6
	7	8	9

Transposed matrix:
	1	4	7
	2	5	8
	3	6	9

An incorrect implementation that modifies the oriignal aray during display:

public class IncorrectTranspose {
    public static void main(String[] args) {
        int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        
        System.out.println("Original matrix:");
        for (int i = 0; i < originalMatrix.length; i++) {
            for (int j = 0; j < originalMatrix[i].length; j++) {
                System.out.print("\t" + originalMatrix[i][j]);
            }
            System.out.println();
        }
        
        System.out.println("\nTransposed matrix (with side effects):");
        for (int i = 0; i < originalMatrix.length; i++) {
            for (int j = 0; j < originalMatrix[i].length; j++) {
                int temporary = originalMatrix[i][j];
                originalMatrix[j][i] = temporary;
                System.out.print("\t" + originalMatrix[j][i]);
            }
            System.out.println();
        }
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.