Section 8.9. Rectangular Arrays


8.9. Rectangular Arrays

So far, we have discussed one-dimensional (or single-subscripted) arraysarrays that contain one row of values. In this section, we introduce multidimensional (also called multiple-subscripted) arrays, which require two or more indices to identify particular elements. We concentrate on two-dimensional (also called double-subscripted) arrays, or arrays that contain multiple rows of values. There are two types of two-dimensional arraysrectangular and jagged. We discuss jagged arrays in Section 8.12. Rectangular arrays often represent tables of values consisting of information arranged in rows and columns. Each row is the same size, and each column is the same size (hence the term "rectangular"). To identify a particular table element, we specify two indicesby convention, the first identifies the element's row, the second the element's column. Figure 8.15 illustrates a rectangular array, a, containing three rows and four columns. A rectangular array with m rows and n columns is called an m-by-n array; the array in Fig. 8.15 is referred to as a 3-by-4 array.

Figure 8.15. Two-dimensional array with three rows and four columns.


Every element in array a is identified in Fig. 8.15 by an element name of the form a(i, j), where a is the name of the array and i and j are the indices that uniquely identify the row and column of each element in array a. Array indices are zero-based, so the names of the elements in the first row all have a first index of 0; the names of the elements in the fourth column all have a second index of 3.

Declaring and Initializing Rectangular Arrays

A two-dimensional rectangular array numbers with two rows and two columns can be declared and initialized with

 Dim numbers As Integer(,) = New Integer(1,1) {} numbers(0, 0) = 1 ' leftmost element in row 0 numbers(0, 1) = 2 ' rightmost element in row 0 numbers(1, 0) = 3 ' leftmost element in row 1 numbers(1, 1) = 4 ' rightmost element in row 1 


Alternatively, the initialization can be written on one line, as shown below:

 Dim numbers As Integer(,) = New Integer(,) {{1,2}, {3,4}} 


The values are grouped by row in braces, with 1 and 2 initializing numbers(0,0) and numbers(0,1), respectively, and 3 and 4 initializing numbers(1,0) and numbers(1,1), respectively. The compiler determines the number of rows by counting the number of subinitializer lists (represented by the sets of data in curly braces) in the main initializer list. Then the compiler determines the number of columns in each row by counting the number of initializer values in the subinitializer list for that row. In rectangular arrays, each row has the same number of values.

The program in Fig. 8.16 demonstrates the initialization of a rectangular array (array1) and the use of nested For...Next loops to traverse the arrays (i.e., to manipulate every array element).

Figure 8.16. Initializing a rectangular array.

  1  ' Fig. 8.16: RectangularArray.vb  2  ' Initializing a rectangular array.  3  Module RectangularArray  4     Sub Main()  5        ' create rectangular array                      6        Dim array1 As Integer(,)                        7        array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}  8  9        Console.WriteLine("Values in rectangular array1 by row are ") 10 11        ' output array1 elements                           12        For i As Integer = 0 To array1.GetUpperBound(0)    13           For j As Integer = 0 To array1.GetUpperBound(1) 14              Console.Write(array1(i, j) & " ")            15           Next                                            16         17           Console.WriteLine()                             18        Next                                               19     End Sub ' Main 20  End Module ' RectangularArray 

 Values in rectangular array1 by row are 1  2  3 4  5  6 



The program declares a rectangular array in method Main. The allocation of rectangular array1 (line 7) provides six initializers in two sublists. The first sublist initializes row 0 of the array to the values 1, 2 and 3; the second sublist initializes row 1 of the array to the values 4, 5 and 6.

The nested For...Next statements in lines 1218 display the elements of rectangular array array1, traversing the array in two dimensions. The outer For...Next statement traverses the rows; the inner For...Next statement traverses the columns within a row. Each For...Next statement calls method GetUpperBound to obtain the upper bound of the dimension it traverses. Note that the dimensions are zero-based, meaning that the rows are dimension 0 (line 12) and the columns are dimension 1 (line 13).



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net