30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 Arrays 281<br />

The program in Fig. 7.16 demonstrates the initialization of a rectangular array<br />

(array1) and a jagged array (array2) in declarations and the use of nested For/Next<br />

loops <strong>to</strong> traverse the arrays (i.e., <strong>to</strong> manipulate every array element).<br />

The program declares two arrays in method Main. The allocation of array1 (line 14)<br />

provides six initializers in two sublists. The first sublist initializes the first row (row 0) of the<br />

array <strong>to</strong> the values 1, 2 and 3; the second sublist initializes the second row (row 1) of the array<br />

<strong>to</strong> the values 4, 5 and 6. The declaration and allocation of array2 (line 17) create a jagged<br />

array of 3 arrays (specified by the 2 in the first set of parentheses after keyword Integer).<br />

Lines 18–20 initialize each subarray so that the first subarray contains the values 1 and 2, the<br />

second contains the value 3 and the last contains the values 4, 5 and 6.<br />

The nested For/Next structures in lines 24–31 append the elements of array1 <strong>to</strong><br />

string output. The nested For/Next structures traverse the arrays in two dimensions.<br />

The outer For/Next structure traverses the rows; the inner For/Next structure traverses<br />

the columns within a row. Each For/Next structure calls method GetUpperBound <strong>to</strong><br />

obtain the upper bound of the dimension it traverses. Notice that the dimensions are zerobased,<br />

meaning the rows are dimension 0 and the columns are dimension 1.<br />

1 ' Fig. 7.16: MultidimensionalArrays.vb<br />

2 ' Initializing multi-dimensional arrays.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modMultidimensionalArrays<br />

7<br />

8 Sub Main()<br />

9 Dim output As String<br />

10 Dim i, j As Integer<br />

11<br />

12 ' create rectangular two-dimensional array<br />

13 Dim array1 As Integer(,)<br />

14 array1 = New Integer(,) {{1, 2, 3}, {4, 5, 6}}<br />

15<br />

16 ' create jagged two-dimensional array<br />

17 Dim array2 As Integer()() = New Integer(2)() {}<br />

18<br />

19 array2(0) = New Integer() {1, 2}<br />

20 array2(1) = New Integer() {3}<br />

21 array2(2) = New Integer() {4, 5, 6}<br />

22<br />

23 output = "Values in array1 by row are " & vbCrLf<br />

24<br />

25 For i = 0 To array1.GetUpperBound(0)<br />

26<br />

27 For j = 0 To array1.GetUpperBound(1)<br />

28 output &= array1(i, j) & " "<br />

29 Next<br />

30<br />

31 output &= vbCrLf<br />

32 Next<br />

33<br />

Fig. Fig. 7.16 7.16 Initializing multidimensional arrays (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!