numberofrows
and numberofcolumns may be integer variables or
constants.
Declaration example:
int[][] scores = new int[24][6];
The data type of this array is either
int[][]
In Java, all two dimensional
arrays of the same intrinsic or Class datatype are the same. int[][] specifies
any two dimensional integer array, no matter what the actual dimensions.
Indexes in both dimensions are implied, and always start
with zero
Dereferencing an array to get a specific
variable: arrayname[row][column]
Example:
scores[14][3] (pronounced "scores sub fourteen, three"
or "scores sub fourteen, sub three")
The indexes can be variables, constants,
or a mix
Example:
scores[i][2]
In Java, a two dimensional array is an
array of one dimensional arrays, and is actually stored that way in memory.
Example:
int[][] scores; // just creates a single null
reference of data type int[][]
int[][] scores = new int[6][]; // creates an
array of six null references of data type int[]
int[][] scores = new int[6][24]; // creates an
array of six references, each to an array of 24 ints.
In Java, you can replace any of the
second dimension arrays with an array of a different size. These are
called jagged arrays.
For example:
int[][] days
= new int[12][];
days[ 0] =
new int[31];
days[ 1] =
new int[29];
days[ 2] =
new int[31];
days[ 3] =
new int[30];
days[ 4] =
new int[31];
days[ 5] =
new int[30];
days[ 6] =
new int[31];
days[ 7] =
new int[31];
days[ 8] =
new int[30];
days[ 9] =
new int[31];
days[10] =
new int[30];
days[11] =
new int[31];
creates exactly what it
looks like, a jagged array that holds only the number of days in a year
(including leap day, if the year is a leap year)
.
Walking a two dimensional array uses two
nested loops:
int i, j;
for (i = 0; i <
arrayname.length; i++)
{
for (j = 0; j <
arrayname[i].length; j++)
{
// process each element of the array by using arrayname[i][j]
}
}
Make sure you understand the
syntax of the limits of in the loops above:
Since the datatype of arrayname
is int[][]
the outer loop limit, arrayname.length
is the length of the first dimension, or the number of rows in the array.
Since the datatype of arrayname[i]
is int[]
the inner loop limit, arrayname[i].length
is the length of the i_th row in the array. In a jagged array, this
might be different for each iteration of the outer loop.
The above code to walk a two
dimensional array will work with any two dimensional array, including
jagged arrays, no matter what the dimensions. It will not give an
arrayOutOfBoundsException.
Passing a two dimensional array to a
function:
A function call of a two dimensional array
is identical to a one dimensional array: average(scores)
The argument is simply the name of the array,
which in Java's case is a reference to the entire 2D array.
The prototype and definition must include
the two dimensional array data type in the formal parameters
double average(int
scores[][]);
Two dimensional arrays in memory are
stored as an array of one dimensional arrays. Each one dimensional array
can be placed anywhere in memory, and do not have to stored together in Java.
C and C++ arrays
The arrays must be rectangle in shape,
with fixed integer sizes for each dimension. Each dimension may have a different
size.
The elements of the array are always
stored one right
after another in memory, in row major order (all of row 0, then all of row 1,
then all of row 2, etc.)
NUMBEROFROWS and
NUMBEROFCOLUMNS must be an integer
constants, usually #define's or const static int's
Declaration example:
int scores[24][6];
The data type of this array is either
int[24][6], or
int[][6]
int[][6] and
int[][14] would be two different datatypes. They are not
compatible.
int[16][6]
and int[8][6]
would be the same datatype, however, because the first dimension does not matter
in the datatype.
The prototype and definition must include
the two dimensional array data type in the formal parameters, with all but
the first array dimension included.
double average(int
scores[][NUMBEROFCOLUMNS]);
Note that NUMBEROFCOLUMNS is required in
this prototype. NUMBEROFROWS, the first dimension, is optional. Only
the first dimension is optional.
Walking a two dimensional array uses two
nested loops:
int i, j;
for (i = 0; i <
NUMBEROFROWS; i++)
{
for (j = 0; j <
NUMBEROFCOLUMNS; j++)
{
// process each element of the array by using arrayname[i][j]
}
}
A dimensional array is stored in memory as
a one dimensional array, with each row one behind another in the array.
The name of the array just points to the beginning of the whole array in memory,
exactly like it does with a one dimensional array.
Its data type, however is different for a
one dimensional array, as stated above.