In java Multi-dimension array defined as the array of array . Multi-dimension include two and three dimensions array.
2 dimension array --- > Collection of 1 Dimension array .
3 dimension array --- > Collection of 2-D array --- > Collection of 1- D array.
2-D Array
It is the collection of 1-D array . By defining two indexes in an array is called as 2-S array and its mandatory to indicate the size of the first index because its the starting point of any types of array, unless you give the 1st index size how it is going to allocate the memory for the second index as because 2-D is the collection of 1-D array.
Declaration of reference variable for 2-D array :
< data_type > < ref var_name > [ ] [ ] ;
< data_type > [ ] [ ] < ref var_name > ;
< data_type > [ ] [ ] < ref var_name > ;
< data_type > [ ] < ref var_name > [ ] ;
< data_type > [ ] < ref var_name > [ ] ;
Creating the array Object :
< ref var_name > = new < data_type > [ size 1 ] [ size 2 ] ;
Note 1: Don't indicate any while creating any reference because reference will only create only reference memory , it is not going to create any array object unless we use new operator to create array.
Note 2: While creating any 2-D array object it is mandatory to mention 1st index and from 2nd index onward size can be ignored.
Why is it mandatory to maintain the 1st index size ?
It is because in case of 2-D array 1st index will hold the reference of the the next dimension array, that means here in 2-D each first index will hold the address of its attached 1-D array which contain the actual value.
Diagrammatic representation of 2-D Array
int values [ ] [ ] = new int [ 3 ] [ 2 ] ;
This is the representation of 2-D array where reference variable " value " pointing to the array object and 1st index holds the reference value for its corresponding 1-D arrays and that array contain the original value .
Here we can see that each array contain its final length property holding the size of the array.
Some of the Invalid statement for 2-D Array :
int values [ ] [ ] = null ;
values = new int [ ] [ ] ; // invalid array object because without any index .
values = new int [ ] [ 2 ] ; // invalid array object because without 1st index .
int values [ 3 ] [ 2 ] = null ; // invalid array object as reference is with indexes.
Some of the Valid statement for 2-D Array :
int values [ ] [ ] = null ;
values = new int [ 3 ] [ ] ; // 2nd index can be left blank and that means 1st index in
not containing any reference value as 2nd index is blank
means no array .
Diagrammatic representation of some of the valid statement of the 2-D Array
int values [ ] [ ] = null ;
Here we can see that s imply a reference variable is created containing null , and hence not pointing to any 2-D array object.
Here we see that as per the statement a 1-D array is created containing null value and not pointing to its corresponding arrays because of 2nd index is left blank.
values [ 0 ] = new int [ 2 ] ;
Here we tried to store a 1-D store in the 1st index of the created array at the previous step. So now it is referencing to a 1-D array of size 2 containing zero as a default value because of int type array.
int tmp [ ] = { 10 , 20 } ;
values [ 1 ] = tmp ;
The above statement indicates that we can even create our own 1-D and can store its reference to any valid 1st index of 2-D array because it contains only the reference of its referencing 1-D array but type of array must be same .
Note that here we are storing the reference the 1-D reference to value [ 1 ] which was containing null previously .
Like this we can also store any 1-D array of any length to value [ 2 ] which is still containing null value. for ex-
values [ 2 ] = new int [ ] { 10, 20, 30 } ;
2 dimension array --- > Collection of 1 Dimension array .
3 dimension array --- > Collection of 2-D array --- > Collection of 1- D array.
2-D Array
It is the collection of 1-D array . By defining two indexes in an array is called as 2-S array and its mandatory to indicate the size of the first index because its the starting point of any types of array, unless you give the 1st index size how it is going to allocate the memory for the second index as because 2-D is the collection of 1-D array.
Declaration of reference variable for 2-D array :
< data_type > < ref var_name > [ ] [ ] ;
< data_type > [ ] [ ] < ref var_name > ;
< data_type > [ ] [ ] < ref var_name > ;
< data_type > [ ] < ref var_name > [ ] ;
< data_type > [ ] < ref var_name > [ ] ;
Creating the array Object :
< ref var_name > = new < data_type > [ size 1 ] [ size 2 ] ;
Note 1: Don't indicate any while creating any reference because reference will only create only reference memory , it is not going to create any array object unless we use new operator to create array.
Note 2: While creating any 2-D array object it is mandatory to mention 1st index and from 2nd index onward size can be ignored.
Why is it mandatory to maintain the 1st index size ?
It is because in case of 2-D array 1st index will hold the reference of the the next dimension array, that means here in 2-D each first index will hold the address of its attached 1-D array which contain the actual value.
Diagrammatic representation of 2-D Array
int values [ ] [ ] = new int [ 3 ] [ 2 ] ;
This is the representation of 2-D array where reference variable " value " pointing to the array object and 1st index holds the reference value for its corresponding 1-D arrays and that array contain the original value .
Here we can see that each array contain its final length property holding the size of the array.
Some of the Invalid statement for 2-D Array :
int values [ ] [ ] = null ;
values = new int [ ] [ ] ; // invalid array object because without any index .
values = new int [ ] [ 2 ] ; // invalid array object because without 1st index .
int values [ 3 ] [ 2 ] = null ; // invalid array object as reference is with indexes.
Some of the Valid statement for 2-D Array :
int values [ ] [ ] = null ;
values = new int [ 3 ] [ ] ; // 2nd index can be left blank and that means 1st index in
not containing any reference value as 2nd index is blank
means no array .
Diagrammatic representation of some of the valid statement of the 2-D Array
int values [ ] [ ] = null ;
Here we can see that s imply a reference variable is created containing null , and hence not pointing to any 2-D array object.
values = new int [ 3 ] [ ] ;
values [ 0 ] = new int [ 2 ] ;
Here we tried to store a 1-D store in the 1st index of the created array at the previous step. So now it is referencing to a 1-D array of size 2 containing zero as a default value because of int type array.
int tmp [ ] = { 10 , 20 } ;
values [ 1 ] = tmp ;
The above statement indicates that we can even create our own 1-D and can store its reference to any valid 1st index of 2-D array because it contains only the reference of its referencing 1-D array but type of array must be same .
Note that here we are storing the reference the 1-D reference to value [ 1 ] which was containing null previously .
Like this we can also store any 1-D array of any length to value [ 2 ] which is still containing null value. for ex-
values [ 2 ] = new int [ ] { 10, 20, 30 } ;
EmoticonEmoticon