"A two-dimensional array consists of an array of one-dimensional arrays and a three-dimensional array consists of an array of two-dimensional arrays."
To give you some examples of three-dimensional arrays, lets first start off with the simplest, a one-dimensional array.
int[] a = {1, 2};
In this case, a is an array containing two values, 1 and 2.
a[0] = 1;
a[1] = 2;
Moving on to two dimensional arrays.
int[][] a = {
{1, 2},
{3, 4}
};
The above example was only written that way to give you a better understanding of what a two-dimensional array looks like. It can also be written like this:
int[][] a = {{1, 2}, {3, 4}};
I personally prefer the top method since you can clearly see what's going on.
a[0] = the array {1, 2}
a[1] = the array {3, 4}
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
And finally a three dimensional array
int[][][] a = {
{
{1, 2},
{3, 4},
}, {
{5, 6},
{7, 8},
}
};
So let's break this down
a[0] contains the array containing the two arrays {1, 2} and {3, 4}
a[1] contains the array containing the two arrays {5, 6} and {7, 8}
a[0][0] contains the array {1, 2}
a[0][1] contains the array {3, 4}
a[1][0] contains the array {5, 6)
a[1][1] contains the array {7, 8}
a[0][0][0] = 1;
a[0][0][1] = 2;
a[0][1][0] = 3;
a[0][1][1] = 4;
a[1][0][0] = 5;
a[1][0][1] = 6;
a[1][1][0] = 7;
a[1][1][1] = 8;
Here's an example of a class that tries to guess your birth-day.
To give you some examples of three-dimensional arrays, lets first start off with the simplest, a one-dimensional array.
int[] a = {1, 2};
In this case, a is an array containing two values, 1 and 2.
a[0] = 1;
a[1] = 2;
Moving on to two dimensional arrays.
int[][] a = {
{1, 2},
{3, 4}
};
The above example was only written that way to give you a better understanding of what a two-dimensional array looks like. It can also be written like this:
int[][] a = {{1, 2}, {3, 4}};
I personally prefer the top method since you can clearly see what's going on.
a[0] = the array {1, 2}
a[1] = the array {3, 4}
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;
And finally a three dimensional array
int[][][] a = {
{
{1, 2},
{3, 4},
}, {
{5, 6},
{7, 8},
}
};
So let's break this down
a[0] contains the array containing the two arrays {1, 2} and {3, 4}
a[1] contains the array containing the two arrays {5, 6} and {7, 8}
a[0][0] contains the array {1, 2}
a[0][1] contains the array {3, 4}
a[1][0] contains the array {5, 6)
a[1][1] contains the array {7, 8}
a[0][0][0] = 1;
a[0][0][1] = 2;
a[0][1][0] = 3;
a[0][1][1] = 4;
a[1][0][0] = 5;
a[1][0][1] = 6;
a[1][1][0] = 7;
a[1][1][1] = 8;
Here's an example of a class that tries to guess your birth-day.
- The dates array stores a three-dimensional array.
- The user is prompted to enter 0 or 1 depending if he sees his birthday within the set currently displayed
- If so, the dates[i][0][0] value is added to the answers variable
- The birth-day is revealed
Comments
Post a Comment