Information Technology Reference
In-Depth Information
One-Dimensional and Rectangular Arrays
Syntactically, one-dimensional arrays and rectangular arrays are very similar, so they will be
treated together. Jagged arrays will be treated separately.
Declaring a One-Dimensional Array or a Rectangular Array
To declare a one-dimensional or rectangular array, use a single set of square brackets between
the type and the variable name.
The rank specifiers are commas between the brackets. They specify the number of dimen-
sions the array will have. The rank is the number of commas, plus one. For example, no
commas indicates a one-dimensional array, one comma indicates a two-dimensional array,
and so forth.
The base type, together with the rank specifiers, is the type of the array. For example, the
following line of code declares a one-dimensional array of long s. The type of the array is
long[] , which is read as “an array of long s.”
Rank specifiers = 1
long [ ] secondArray;
Array type
The following code shows examples of declarations of rectangular arrays. Notice that
￿
You can have as many rank specifiers as you need.
￿
You cannot place array dimension lengths in the array type section. The rank is part of
the array's type, but the lengths of the dimensions are not part of the type.
￿
When an array is declared, the number of dimensions is fixed. The length of the dimen-
sions, however, is not determined until the array is instantiated.
Rank specifiers
int[,,] firstArray; // Array type: 3-D array of int
int[,] arr1; // Array type: 2-D array of int
long[,,] arr3; // Array type: 3-D array of long
Array type
long[3,2,6] SecondArray; // Wrong! Compile error
Dimension lengths not allowed!
Note Unlike C/C++, the brackets follow the base type, not the variable name.
Search WWH ::




Custom Search