Multidimensional Arrays
In this chapter you will learn:
- How is the multidimensional arrays stored
- How to create a three-dimensional array
- What are Jagged array
- How to initialize multidimensional arrays during declaration
Multidimensional Arrays and its form
In Java, multidimensional arrays are actually arrays of arrays.
For example, the following declares a two-dimensional array variable called twoD
.
int twoD[][] = new int[4][5];
This allocates a 4-by-5 array and assigns it to twoD. This array will look like the one shown in the following:
The wrong way to think about multi-dimension arrays
right way to think about multi-dimension arrays
An irregular multi-dimension arrayThe following code use nested for loop to assign values to a two-dimensional array.
public class Main {
public static void main(String args[]) {
int twoD[][] = new int[4][5];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
twoD[i][j] = i*j;// j ava2s . c om
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
}
}
This program generates the following output:
An example of a three-dimensional array
The following program creates a 3 by 4 by 5, three-dimensional array.
public class Main {
public static void main(String args[]) {
int threeD[][][] = new int[3][4][5];
// j a va 2 s . c om
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 5; k++)
threeD[i][j][k] = i * j * k;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 5; k++)
System.out.print(threeD[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
This program generates the following output:
Jagged array
When you allocate memory for a multidimensional array, you can allocate the remaining dimensions separately. For example, the following code allocates the second dimension manually.
public class Main {
public static void main(String[] argv) {
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
}// j a v a2 s. c om
}
When allocating dimensions manually, you do not need to allocate the same number of elements for each dimension.
The following program creates a two-dimensional array in which the sizes of the second dimension are unequal.
public class Main {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
//from j a v a2s.com
for (int i = 0; i < 4; i++){
for (int j = 0; j < i + 1; j++) {
twoD[i][j] = i + j;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i + 1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
This program generates the following output:
The array created by this program looks like this:
Initialize multidimensional arrays
We can initialize multidimensional arrays during declaration by enclosing each dimension's initializer within its own set of curly braces.
public class Main{
public static void main(String args[]) {
double m[][] = {
{ 0, 1, 2, 3 }, // ja v a 2 s . c om
{ 0, 1, 2, 3 },
{ 0, 1, 2, 3 },
{ 0, 1, 2, 3 }
};
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++){
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
}
When you run this program, you will get the following output:
Next chapter...
What you will learn in the next chapter:
- Calculate Average value of Array elements
- How to Create Fibonacci Series with array
- How to use two-dimensional double type array to do Matrix calculation