w3resource

Java: Print an array after changing the rows and columns of a specified two-dimensional array


Transpose 2D Array

Write a Java program to print an array after changing the rows and columns of a two-dimensional array.

Original Array:
10 20 30
40 50 60
After changing the rows and columns of the said array:10 40
20 50
30 60

Visual Presentation:

Java Basic Exercises: Print an array after changing the rows and columns of a specified two-dimensional array.


Sample Solution:

Java Code:

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        // Initializing a 2D array with values
        int[][] twodm = {
                {10, 20, 30},
                {40, 50, 60}
        };
        
        // Displaying the original array
        System.out.print("Original Array:\n");
        print_array(twodm);
        
        // Performing transpose operation on the array
        System.out.print("After changing the rows and columns of the said array:");
        transpose(twodm);
    }
    
    // Method to transpose the given 2D array
    private static void transpose(int[][] twodm) {
        // Creating a new 2D array to store the transposed elements
        int[][] newtwodm = new int[twodm[0].length][twodm.length];
        
        // Transposing the elements of the array
        for (int i = 0; i < twodm.length; i++) {
            for (int j = 0; j < twodm[0].length; j++) {
                newtwodm[j][i] = twodm[i][j];
            }
        }
        
        // Printing the transposed array
        print_array(newtwodm);
    }
    
    // Method to print the elements of a 2D array
    private static void print_array(int[][] twodm) {
        // Looping through the array and printing its elements
        for (int i = 0; i < twodm.length; i++) {
            for (int j = 0; j < twodm[0].length; j++) {
                System.out.print(twodm[i][j] + " ");
            }
            System.out.println();
        }
    }
} 

Sample Output:

Original Array:
10 20 30 
40 50 60 
After changing the rows and columns of the said array:10 40 
20 50 
30 60 

Flowchart:

Flowchart: Java exercises: Print an array after changing the rows and columns of a specified two-dimensional array.



For more Practice: Solve these Related Problems:

  • Write a Java program to transpose a non-square matrix and print the resulting array.
  • Write a Java program to perform an in-place transpose of a square matrix without using extra space.
  • Write a Java program to transpose a 2D array and then reverse each row of the transposed matrix.
  • Write a Java program to transpose a square matrix and calculate the sum of its primary diagonal.

Go to:


PREV : Print Boolean Matrix.
NEXT : Largest Integer Base-2 Logarithm.


Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.