Java Multidimensional Arrays
In this chapter you will learn:
- How is the multidimensional arrays stored
- Syntax for Java Multidimensional Arrays
- Example - Java Multidimensional Arrays
- Example - How to create a three-dimensional array
- What are Jagged array
- Example - Jagged array
- How to initialize multidimensional arrays during declaration
Description
In Java, multidimensional arrays are actually arrays of arrays.
Syntax
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 is as follows.
The right way to think about multi-dimension arrays
Example
The 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;/*from w ww.ja va2 s . c o m*/
}
}
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:
Example 2
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];
//from www. jav a2s .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.
An irregular multi-dimension array
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];
}/*from www . j ava 2 s . c o m*/
}
When allocating dimensions manually, you do not need to allocate the same number of elements for each dimension.
Example 3
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];
/*w ww. j a v a 2 s .c om*/
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:
Example 4
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 }, //ww w.j a v a 2s .c o m
{ 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: