Saturday, September 4, 2010

Multidimensinal Arrays in Java

Hello Hardik,

We can make arrays of 3 or 4 or more dimensions though these are
impossible to draw in two dimensions, and they are bit harder to
imagine in our mind.


In Java language, a multidimensional (two or more dimensional) arrays
are simply an array whose components are themselves arrays (Jagged
arrays) or you can say an array of arrays or an array of arrays of
arrays an so on.

In the Java, arrays are objects (you can say 'reference types'), are
dynamically created. A variable of array type holds a reference to an
object. Declaring a variable of array type does not create an array
object or allocate any space for array components.

for example,

Here is declaration of java arrays:
int []a;
int [][]b;
int [][][]c;

"a","b",and "c" are called object variables, and in case of their
declaration space - when declared inside the methods, these object
variables are uninitialized. If these variables are fields (declared
as members of class) then they have a special null value.

You can create an array using (1) creation expression (a=new int[4])
or (2) array

initializer. (a=new int[]{1,22} or int []a={11,22,33,44}).

Example-1
int [][][]threeD=new int[2][3][7];

Example-2

int [][][]threeD;

//Create an array that can store references of two arrays of
arrays of int

elements
threeD=new int[2][][];

//create an array that can store references of 4 arrays of int
threeD[0]=new int[4][];
//create an array that can store references of 2 arrays of int
threeD[1]=new int[2][];

//create an array that can store n int elements
threeD[0][0]=new int[]{22,33,44};
threeD[0][1]=new int[]{7,6,4,4,74};
threeD[0][2]=new int[10];
threeD[0][3]=new int[]{17,26,34,3,7,4};

threeD[1][0]=new int[]{100,200};
threeD[1][1]=new int[]{10,20,30,40,50,60,70};

Example - 3 Array initializer

int [][][]threeD=
{
{
{22,33,44},
{7,6,4,4,74},
new int[10],
{17,26,34,3,7,4}
},
{
{100,200},
{10,20,30,40,50,60,70}
}
};

*/

public class Sample {
public static void main(String []args) {
//There are 3 class rooms, each class room has 4 students.
// Write a 3D array to store marks of five subject for each
student.
int [][][]a=new int[3][4][5];
a[0][0][0]=88; //1st class, 1st student and mark of 1st subject.
a[0][0][1]=98; //1st class, 1st student and mark of 2nd subject.
a[0][1][0]=77; //1st class, 2nd student and mark of 1st subject.


//Another way to declare & create 3D.
//marks of five subject of raj & ram (one-dim for each student)
int []raj={55,66,77,66,55};
int []ram={77,88,66,55,98};

//Now declare & create an array to store references of "raj" &
"ram" array
int [][]studs1={raj,ram};

//marks of five subject of foo, bar & boo (one-dim for each student)
int []foo={85,66,77,66,55};
int []bar={37,68,66,55,98};
int []boo={87,48,66,55,98};

//Now declare & create an array to store references of
"foo","bar" & "boo"

array
int [][]studs2={foo,bar,boo}; //2D - Array of oneD array

//Now, declare & create an array that hold the references of
"studs1" and

"studs2"

int [][][]classes={studs1,studs2}; //3D - Array of TwoD array


System.out.println(studs1[0][0]); //marks of 1st sub of raj
System.out.println(studs2[2][0]); //marks of 1st sub of boo

System.out.println(classes[0][0][0]); // marks of 1st class
of 1st sub of raj


//Print array elements
for(int i=0;i

Thursday, September 2, 2010