PART I
C h a p t e r 7 :
A r r a y s a n d S t r i n g s
141
PART IPART I
Boundaries Are Enforced
Array boundaries are strictly enforced in C#; it is a runtime error to overrun or underrun
the ends of an array. If you want to confirm this for yourself, try the following program that
purposely overruns an array:
// Demonstrate an array overrun.
using System;
class ArrayErr {
static void Main() {
int[] sample = new int[10];
int i;
// Generate an array overrun.
for(i = 0; i < 100; i = i+1)
sample[i] = i;
}
}
As soon as
i
reaches 10, an
IndexOutOfRangeException
is generated and the program is
terminated. (See Chapter 13 for a discussion of exceptions and exception handling.)
Multidimensional Arrays
Although the one-dimensional array is the most commonly used array in programming,
multidimensional arrays are certainly not rare. A
multidimensional array
is an array that has
two or more dimensions, and an individual element is accessed through the combination
of two or more indices.
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional array. In a two-
dimensional array, the location of any specific element is specified by two indices. If you
think of a two-dimensional array as a table of information, one index indicates the row, the
other indicates the column.
To declare a two-dimensional integer array
table
of size 10, 20, you would write
int[,] table = new int[10, 20];
Pay careful attention to the declaration. Notice that the two dimensions are separated from
each other by a comma. In the first part of the declaration, the syntax
[,]
indicates that a two-dimensional array reference variable is being created. When memory is
actually allocated for the array using
new
, this syntax is used:
int[10, 20]
This creates a 10×20 array, and again, the comma separates the dimensions.
www.freepdf-books.com
142
P a r t I :
T h e C # L a n g u a g e
To access an element in a two-dimensional array, you must specify both indices,
separating the two with a comma. For example, to assign the value 10 to location 3, 5
of array
table
, you would use
table[3, 5] = 10;
Here is a complete example. It loads a two-dimensional array with the numbers 1
through 12 and then displays the contents of the array.
// Demonstrate a two-dimensional array.
using System;
class TwoD {
static void Main() {
int t, i;
int[,] table = new int[3, 4];
for(t=0; t < 3; ++t) {
for(i=0; i < 4; ++i) {
table[t,i] = (t*4)+i+1;
Console.Write(table[t,i] + " ");
}
Console.WriteLine();
}
}
}
In this example,
table[0, 0]
will have the value 1,
table[0, 1]
the value 2,
table[0, 2]
the
value 3, and so on. The value of
table[2, 3]
will be 12. Conceptually, the array will look like
the one shown in Figure 7-1.
N
OTE
N
OTE
If you have previously programmed in C, C++, or Java, be careful when declaring or accessing
multidimensional arrays in C#. In these other languages, array dimensions and indices are
specified within their own set of brackets. C# separates dimensions using commas.
Arrays of Three or More Dimensions
C# allows arrays with more than two dimensions. Here is the general form of a
multidimensional array declaration:
type
[,
...
,]
name =
new
type
[
size1
,
size2
, ...,
sizeN
];
F
IGURE
7-1
A conceptual view
of the
table
array
created by the
TwoD
program
www.freepdf-books.com
Do'stlaringiz bilan baham: |