TwoD Array - Intro
TwoD Array - Intro
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
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( ) ;
} // 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