Multidimensional array is used for insert values in a table format like rows and columns. The Array can have more than one dimension. In the last lesson we discuss about one dimension array.In this lesson discuss about array in more than one dimension, two dimensional,three dimensional... like that.When you want to insert two or more values with relation in an array, it's difficult to do using in singly array.
Ex:- Store the student marks for each student for each subject,
Table format representation is -
The above example can represent using a two dimensional array. It's can declare, subject and Student No is using dimensions in the array.
Create Multidimensional array Object:-
Array object can be created in two methods, the first one is using new key word and another one is initializing the contents.
Using new key word-
DATATYPE VARIABLENAME [][]....=new DATATYPE[SIZE-OF-FIRST-DIMENSION][SIZE-OF-SECOND-DIMENSION]....;
Using initializing the content-
DATATYPE VARIABLENAME [][]....={{{value,value},{value,value..},{{values},{values},..}}};
Create two-dimensional array object:-
Two dimensional arrays can represent table format. Two dimensional array and three dimensional array are most popular arrays in a multidimensional array. Because when we create up three dimensional arrays, it's more complex.
Using new key word-
Structure -
DATATYPE VARIABLENAME [][]=new DATATYPE[SIZE][SIZE];
Ex:-
int array[][]=new int[3][5];
String names[][]=new String[5][5];
Using initializing the content-
Ex:-
int array[][]={{2,6,3,2},{1,6,9,2},{2,5},{8}};
Above table two dimensional array example is can represent like this -
char marks[][]={{'A','B','E','C'},{'D','A','E','D'},{'C','C','E','C'},{'A','B','B','B'},{'A','A','A','A'}};
Structure -
DATATYPE VARIABLENAME [][]={{value,value..},{values,value....},{value...}};
Ex:-
int array[][]={{2,6,3,2},{1,6,9,2},{2,5},{8}};
Above table two dimensional array example is can represent like this -
char marks[][]={{'A','B','E','C'},{'D','A','E','D'},{'C','C','E','C'},{'A','B','B','B'},{'A','A','A','A'}};
Create three-dimensional array object:-
Three dimensional array declaration is like to two dimensional array declaration,
Using new keyword-
Structure -
DATATYPE VARIABLENAME [][][]=new DATATYPE[SIZE][SIZE][SIZE];
Ex-
int names[][][]=new int[2][3][7];
Using initializing the content-
Structure-
DATATYPE VARIBLENAME [][][]={{{VALUE},{VALUE}},{{VALUE},{VALUE}},{{VALUE}}}
**Colors are use for represent array scope
Ex-
int names[][][]={{{2,4,6},{20,4}},{{5,23,64}}};
Values are represented in array index.
Structure-
VARIABLENAME [INDEX][INDEX]...;
Ex-
two dimensional array-
array[2][4];
three dimensional array-
array[3][5][8];
For additional details about this are included in the examples given below.
Example One-
public class Test {
public static void main(String args[]){
System.out.println(" *********www.aicotutorial.com******** ");
String array[][]={{"Jack","Mary"},{"Roce","Bill","Boston"},{"wiliam"}};
for(int i=0;i<array.length;i++){
if(i==0){
System.out.println();
System.out.println("first row......");
for(int j=0;j<2;j++)
System.out.print(j+" index -"+array[i][j]+" ,");
}
else if(i==1){
System.out.println();
System.out.println("second row......");
for(int j=0;j<3;j++)
System.out.print(j+" index -"+array[i][j]+" ,");
}
else{
System.out.println();
System.out.println("third row.......");
for(int j=0;j<1;j++)
System.out.println(j+" index -"+array[i][j]+" ,");
}
}
}
}
Above Java code is represented how to create a two dimensional array and how to access those values using nested for loop. First create a two dimensional array and store values in array declaration time.
Then using for loops print that array. Array index are identified using row and each row column index like array [2][3]=first row 2 index.
Example Two-
public class website {
public static void main(String args[]){
System.out.println(" *********www.aicotutorial.com******** ");
int array[][]=new int[4][3]; //declare the array
array[0][0]=1; //store the values
array[0][1]=2;
array[1][2]=3;
array[3][0]=4;
array[2][2]=5;
array[3][1]=6;
array[1][1]=7;
for(int i=0;i<4;i++) { //print the array using for loop
System.out.println();
System.out.println(" row "+i+"......");
for(int j=0;j<3;j++){
System.out.println(j+" index ="+array[i][j]+" ,");
}
}
}
}
In this example represent two dimensional array. First declare the array. Then store values one by one for each index.
This example table representation is given below-
Index
|
0
|
1
|
2
|
3
|
0
|
1
|
4
|
||
1
|
2
|
7
|
6
|
|
2
|
4
|
5
|
Example Three-
public class website {
public static void main(String args[]){
System.out.println(" *********www.aicotutorial.com******** ");
int array[][]=new int[2][]; //declare the array
array[0]=new int[2]; //declare the array index 0
array[1]=new int[5]; //declare the array index 1
array[0][0]=1; //store the values
array[0][1]=2;
array[1][0]=3;
array[1][1]=4;
array[1][2]=5;
array[1][3]=6;
array[1][4]=7;
for(int i=0;i<array.length;i++){ //print the array
if(i==0){
System.out.println();
System.out.println("first row......");
for(int j=0;j<2;j++){
System.out.println(j+" index ="+array[i][j]+" ,");
}
}
else{
System.out.println();
System.out.println("second row......");
for(int j=0;j<5;j++){
System.out.println(j+" index ="+array[i][j]+" ,");
}
}
}
}
}
This example shows that how to create arrays with different length for each index.
Table representation is -
Table representation is -
Index
|
0
|
1
|
0
|
1
|
3
|
1
|
2
|
4
|
2
|
5
|
|
3
|
6
|
|
4
|
7
|
No comments:
Post a Comment