0% found this document useful (0 votes)
4 views4 pages

TwoD Array - Intro

The document explains the concept of two-dimensional arrays (2D arrays) in Java, detailing their structure as collections of one-dimensional arrays and their representation as matrices. It provides syntax for declaring, initializing, and accessing elements in both fixed and variable length 2D arrays, along with examples and methods for displaying matrix properties such as transposes and diagonals. Additionally, it includes a sample Java program demonstrating how to input and display a 2D array and its transpose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

TwoD Array - Intro

The document explains the concept of two-dimensional arrays (2D arrays) in Java, detailing their structure as collections of one-dimensional arrays and their representation as matrices. It provides syntax for declaring, initializing, and accessing elements in both fixed and variable length 2D arrays, along with examples and methods for displaying matrix properties such as transposes and diagonals. Additionally, it includes a sample Java program demonstrating how to input and display a 2D array and its transpose.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Two Dimensional Array ( 2D Array)

It is a collection of 1D Array. It is can be used to represent a Matrix. Java support both fixed length and variable length 2D array. For
Example: A matrix of Order 3 x 5 means 3 Rows and 5 Columns ( i.e. 3 OneD Array and each 1D Array have 5 Elements )
As per the Representation to depict it in a Matrix Format 3 x 5

0 1 2 3 4
0 25 9 6 21 7
1 15 25 6 96 10
2 2 36 9 7 82
But Internally, each 1D array is placed after another 1D array ( like below picture ):

0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
25 9 6 21 7 15 25 6 96 10 2 36 9 7 82
Row 0 Row 1 Row 2
  
7890 7910 7930
Base Address Base Address Base Address

Syntax : To Declare a 2D array Initialized with default Initial values as per data type :
DataType arrayName [ ] [ ] = new DataType [ R ] [ C ] ;
Here , R means Total No. of Rows or No. of 1D Array
C means Total No. of Columns or No. of Elements in each 1D Array
Syntax : To access an specific element of 2D array :
ArrayName [ Row No ] [ Column No ]
Syntax : To Refer Specific Row ( Nth OneD Array ) of a 2D Array : ArrayName[ Row No ]
Syntax : To Refer Total No of Rows : ArrayName . length
Syntax : To Refer Total No of Columns : ArrayName[ 0 ] . length ( Fixed Length Array)
Or
ArrayName[ RowNo ] . length ( Variable Length Array)
Example: 2D Array Initialization :
int ARR [ ] [ ] = { { 21, 7, 87 } , { 55, 102, 6, 7 } , { 71, 82, 19, 15, 277 } } ; // Variable Length Array
int arr [ ] [ ] = { { 21, 7, 87 } , { 5, 6, 7 } , { 71, 19, 83 } }; // Fixed Length Array

Example: Declaration of Fixed Length 2D Array:


int NUM [ ][ ] = new int [ 3 ] [ 5 ] ; // 2D Array with 3 Rows Matrix Order 3 x 5

Example: Declaration of Variable Length 2D Array:


int ARR [ ][ ] = new int [ 3 ] [ ] ; // 2D Array with 3 Rows
ARR[ 0 ] = new int [ 3 ] ; // 0th Row of 2D Array with 3 Elements
ARR[ 1 ] = new int [ 4 ] ; // 1th Row of 2D Array with 4 Elements
ARR[ 2 ] = new int [ 5 ] ; // 2th Row of 2D Array with 5 Elements
Example: To Access an element of 2D Array:
S.o.p.( ARR[ 2 ] [ 3 ] ) ;  15

The Ways to Access the values of 2D Array:


 Row Major Order ( Row Wise Access )
 Column Major Order ( Column Wise Access )

0 1 2 3 0 1 2 3

0 0, 0 0, 1 0, 2 0, 3
0 25 9 6 21
1 1, 0 1, 1 1, 2 1, 3
1 15 85 6 96
2 2, 0 2, 1 2, 2 2, 3
2 2 36 9 7
3 3, 0 3, 1 3, 2 3, 3
3 15 8 17 10
Program to display the Transpose of a given matrix.

import java.util.*;
class Transpose_Display
{
public static void main( )
{
Scanner SC = new Scanner( System.in );
int ROW, COL, m , k ;
System.out.print( " Enter the No. of Rows required : " );
ROW = SC.nextInt( ) ;
System.out.print( " Enter the No. of Columns required : " );
COL = SC.nextInt( ) ;

int NUM[ ][ ] = new int [ ROW ] [ COL ] ;


System.out.println( " Enter the 2D Array values in Row Major Order : " );
for( m = 0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k = 0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t Enter any number for Array element" );
System.out.print( " [ " + m + " ][ "+ k + " ] : " );
NUM[m][k] = SC.nextInt() ;
}
System.out.println( );
} // end of outer for

System.out.println( "\n Given 2D Array values in Row Major Order : " );


for( m = 0 ; m < NUM.length ; m++ ) // for No of Rows
{
for( k = 0 ; k < NUM[0].length ; k++ ) // For No of Columns
{
System.out.print( "\t" + NUM[m][k] );
}
System.out.println( );
}

System.out.println( "\n Transpose of given 2D Array is : " );


for( m = 0 ; m < NUM[0].length ; m++ ) // For No of Columns
{
for( k = 0 ; k < NUM.length ; k++ ) // for No of Rows
{
System.out.print( "\t" + NUM[k][m] );
} // end of inner for
System.out.println( );
} // end of outer for

} // end of main
} // end of class
Leading Diagonal : Right Diagonal :

0 1 2 3 0 1 2 3

0 25 0 21

1 85 1 6

2 9 2 36

3 10 3 15

To Display Only Leading Diagonal of a Matrix :


System.out.println( "\n Leading (Principle) Diagonal : " );
for( n = 0 ; n < NUM.length ; n++ )
{
for( k = 0 ; k < NUM[0].length ; k++ )
{
if( n == k )
System.out.print( "\t" + NUM[n][k] );
else
System.out.print( "\t" );
}
System.out.println( );
} // end of outer for

To Display Only Right Diagonal of a Matrix :

System.out.println( "\n Right Diagonal : " );


for( n = 0 ; n < NUM.length ; n++ )
{
for( k = 0 ; k < NUM[0].length ; k++ )
{
if( n+k == NUM.length-1 )
System.out.print( "\t" + NUM[n][k] );
else
System.out.print( "\t" );
}
System.out.println( );
} // end of outer for

You might also like