Multi-Dimensional Arrays |
|
|
Two-Dimensional Arrays One-dimensional arrays resemble lists. Two-dimensional arrays resemble a table or matrix with rows and columns. ══════════════════ Declaration Multi-dimensional arrays are declared like one-dimensional arrays.
══════════════════ examples: Dim matrix (4, 2) As Integer
Array matrix is a 2-dimensional array with 5 rows and 3 columns--or 15 elements. ══════════════════ Elements in 2-D arrays are referred to by the array variable name and two indexes--one for each dimension. ex.
matrix
(3,2) Using the declaration above, along with the following declarations: Dim rowCtr As Integer, colCtr As Integer the program segment below will create a portion of a multiplication table: For rowCtr = 0 to
matrix.GetUpperBound(0)
Notice that one row was processed at a time. Since the loop that contained rowCtr as a loop control variable was the outermost loop, the inner loop was processed completely for each increment of rowCtr, i.e., rowCtr =1: colCtr = 1, 2, 3; rowCtr = 2: colCtr = 1, 2, 3; etc. In nested loops the innermost loop runs faster. Note: For loops are ideal for processing an entire array. However, if you want to stop processing when a certain condition is met then the For loop should not be used. The For loop is a count-controlled loop, and should include no additional terminating conditions. In such cases a While loop is preferable. ══════════════════ The following code demonstrates the necessity of the While loop. It creates a partial multiplication table from a 6 x 6 array (dimensioned as prod(5, 5)) with the added restriction that the results can be no more than single digits: For rowCtr = 0 to
prod.GetUpperBound(0) ══════════════════ GetUpperBound -- The complete syntax for GetUpperBound is arrayname.GetUpperBound(dimension) The dimension argument is a whole number indicating which dimension's upper bound is returned. Use 0 for the first dimension, 1 for the second, and so on. So 0 corresponds to the row in a two-dimensional array, and 1 corresponds to the column. Dim testArray (100, 3)
Array Processing
testArray(rowCtr, colCtr)
rowCtr = <some value>
colCtr
= <some value>
When using procedures, passing the row and column limits as parameters allows partial processing of arrays. Multi-Dimensional Arrays Visual Basic allows arrays with more than two dimensions. The number of dimensions of an array is the same as the number of indexes needed to reference a particular cell. This example stores sales data for various items in multiple stores for each of the months, or three dimensional data. Dim sales (49, 9, 11) As Single
The total number of elements is the product of the number of elements in each dimension of the array, in this case 50 x 10 x 12, or 6000 elements. One way to keep track of where you are in a multi-dimensional array is to let the first subscript (left-hand one) be referenced by the outermost loop, the second subscript by the next to the outermost loop, etc., until the last subscript is referenced by the innermost loop. Dim cube
(2, 1, 1) As Integer For heightCtr =
0 To cube.GetUpperBound(0)
Note that the widthCtr, or innermost loop, varies most rapidly, then the lengthCtr, then the heightCtr. Always declare an array large enough to hold the maximum number of entries, i.e., make it larger than the anticipated need (but not overly large). ══════════════════ To declare a dynamic two-dimensional array, declare the array with a comma instead of dimensions, and specify the size in the ReDim statement. Dim testArray ( , ) as Integer ReDim testArray (10, 15) Passing Multi-Dimensional Arrays to Procedures To pass an entire multi-dimensional array to a procedure, specify the array name as the argument. For example, if array testArray is declared as Dim testArray(10,15) As Integer the call
passes array testArray to procedure modify2DArray.
Receiving Multi-Dimensional Arrays in Procedures For a procedure to receive an entire array through a call, the parameter list must specify that an array will be received by including a comma in parenthesis (1 comma for a 2D array, 2 commas for a 3D array, etc.). For example, the procedure header for modify2DArray might be written as Private Sub modify2DArray(ByVal receivedArray( , ) As Integer) indicating that modify2DArray expects to receive an Integer array in parameter receivedArray. The size of the array is not specified between the array parentheses, although the number of dimensions is implied. |