java2db

Multidimensional Array (array of arrays) in Java



An array of arrays is called a multidimensional array.The array index always starts with  o(zero). In this case each and every array acts as an element ,it can access by it’s unique index.

Creating ,Initializing and Accessing and copying a Multidimensional  Array :

Multidimensional arrays(array of arrays) in java Two dimensional array : It is an array of one dimensional arrays.

Multidimensional arrays(array of arrays) in java Creation :

Sysntax:

<array_type>  <array_name> [][] = {{element1,element2,…},{element1,element2,…} ,…};      or<array_type>  <array_name> [][] =new <array_type>[lenght][lenght];

Example :

int  [][] int_array = new int[3][6];

Multidimensional arrays(array of arrays) in java Initialization :

int_array [0][0] = 20;
int_array [0][1] = 30;
int_array [0][2] = 25;
int_array [0][3] = 40;
int_array [0][4] = 32;
int_array [0][5] = 15;
int_array [1][0] = 50;
int_array [1][1] = 33;
int_array [1][2] = 42;
int_array [1][3] = 12;
int_array [1][4] = 18;
int_array [1][5] = 26;
int_array [2][0] = 11;
int_array [2][1] = 10;
int_array [2][2] = 55;
int_array [2][3] = 60;
int_array [2][4] = 36;
int_array [2][5] = 22;

//or

int[][] int_array = {{20,30,25,40,32,15},{50,33,42,12,18,26},{11,10,55,60,36,22}};

Multidimensional arrays(array of arrays) in java Two Dimensional Array Representation :

 

 

Multidimensional arrays(array of arrays) in java  Two dimensional array example :

 

 

Multidimensional arrays(array of arrays) in java Three dimensional array: It is an array of two dimensional arrays.

Multidimensional arrays(array of arrays) in java Creation :

 Sysntax:

<array_type>  <array_name> [][][] = {{{element1,element2,…},{element1,element2,…}, ….},{{element1,element2,…},{element1,element2,…},….}, …..};       or<array_type>  <array_name> [][][]=new <array_type>[lenght][lenght][lenght];

 

Example :

int [][][] int_array = new int[2][3][3];

 

Multidimensional arrays(array of arrays) in java Initialization :

int_array[0][0][0] = 30;
int_array[0][0][1] = 40;
int_array[0][0][2] = 25;
int_array[0][1][0] = 15;
int_array[0][1][1] = 10;
int_array[0][1][2] = 35;
int_array[0][2][0] = 20;
int_array[0][2][1] = 22;
int_array[0][2][2] = 42;
int_array[1][0][0] = 11;
int_array[1][0][1] = 9;
int_array[1][0][2] = 16;
int_array[1][1][0] = 32;
int_array[1][1][1] =44 ;
int_array[1][1][2] = 36;
int_array[1][2][0] = 48;
int_array[1][2][1] = 29;
int_array[1][2][2] = 33;

or
int[][][] int_array = {{{30,40,25},{15,10,35},{20,22,42}},{{11,9,16},{32,44,36},{48,29,33}}};

 

Multidimensional arrays(array of arrays) in java Three Dimensional Array Representation :

Three dimensional array of above 18 elements as follows.

 

If we separate the depth 0 and depth 1 , the two parts look like this 

 

Again we are going to separate each and every element from the above diagram.

 

Multidimensional arrays(array of arrays) in java Three dimensional array example :

Note : N-dimensional array is an array of N-1 dimensional arrays.

 

 

Multidimensional arrays(array of arrays) in java Copying multidimensional arrays :

To copy a multidimensional array to an another array, use the below method from java.lang.System class.

public static void arraycopy(Object srcArray,int srcPos,Object destarray,int destPos,int length)

srcArray – the source array.
srcPos – starting position in the source array.
destArray – the destination array.
destPos – starting position in the destination data.
length – the number of array elements to be copied.

Simple example using multidimensional array /arrays:




Related Posts :
   [will not be published]



^ <