# Transpose 2d array java – Java Program to Find the Transpose of a Given Matrix

Transpose 2d array java: In the previous article, we have discussed Java Program to Find Product of All Elements of Matrices

In this article we are going to see how we can write a program to find the transpose of a matrix in JAVA language.

## Java Program to Find the Transpose of a Given Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Transpose matrix represents all its rows values in its column respectively.

The Matrix A  :

19 25 32
40 54 62
70 20 60

Transpose of Matrix A:

19 40 70
25 54 20
32 62 60

Let’s see different ways to Find the Transpose of a Given Matrix.

### Method-1: By Static Initialization of Array Elements

Approach:

• Initialize and declare two arrays of size 3×3, one with elements.
• Use two for loops to iterate the rows and columns .
• Inside the for loops store all array elements in the resultant array such that res[row][col] = arr[col][row].
• Print the resultant array.

Program:

public class matrix{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}, res[][] = new int[3][3];
int row, col ;

System.out.print("The matrix elements are : ");
printMatrix(arr);

// Loops to find transpose of the matrix
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
res[col][row]= arr[row][col];
}
System.out.print("\n\nThe transpose of the matrix : ");
printMatrix(res);
}

// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
}


Output:

The matrix elements are :
19 25 32
40 54 62
70 20 60

The transpose of the matrix :
19 40 70
25 54 20
32 62 60

### Method-2: By Dynamic Initialization of Array Elements

Approach:

• Initialize two arrays of size 3×3.
• Ask the user for input of array elements and store them in the one array using two for loops.
• Use two for loops to iterate the rows and columns .
• Inside the for loops store all array elements in the resultant array such that res[row][col] = arr[col][row].
• Print the resultant array.

Program:

import java.util.Scanner;
public class matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3], res[][] = new int[3][3];
int row, col ;

// Taking matrix input
System.out.println("\nEnter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();

System.out.print("The matrix elements are : ");
printMatrix(arr);

// Loops to find transpose of the matrix
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
res[col][row]= arr[row][col];
}
System.out.print("\n\nThe transpose of the matrix : ");
printMatrix(res);
}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
}


Output:

Enter matrix elements :  41 22 38 91 25 71 83 44 51 63
The matrix elements are :
41 22 38
91 25 71
83 44 51

The transpose of the matrix :
41 91 83
22 25 44
38 71 51

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: