Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Transposing Rows and Columns in Two-Dimensional Arrays

Tech May 15 48

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

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...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Comprehensive Guide to Hive SQL Syntax and Operations

This article provides a detailed walkthrough of Hive SQL, categorizing its features and syntax for practical use. Hive SQL is segmented into the following categories: DDL Statements: Operations on...

Leave a Comment

Anonymous

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