Section 3.4. Arrays

   

3.4 Arrays

The array data type is a fundamental data type in most languages, including Visual Basic. An array is used to store a collection of similar data types or objects.

Many authors of programming books misuse the terms associated with arrays, so let's begin by establishing the correct terminology. In fact, if you will indulge us, we would like to begin with a formal definition of the term array.

3.4.1 Definition of Array

Let S 1 , S 2 ..., S N be finite sets, and let T be a data type (such as Integer). Then an array of type T is a function:

 arr:S  1  S  2  ...  S  N  T 

where S 1 · S 2 · ... · S N is the Cartesian product of the sets S 1 , S 2 ..., S N . (This is the set of all n-tuples whose coordinates come from the sets S i .)

For arrays in VB.NET (and the other languages that implement the Common Language Runtime), the sets S i must have the form:

 S  i  ={0,1,...,K  i  } 

In other words, each set S i is a finite set of consecutive integers starting with 0.

Each position in the Cartesian product is referred to as a coordinate of the array. For each coordinate, the integer K i is called the upper bound of the coordinate. The lower bound is 0 for all arrays in VB.NET.

3.4.2 Dimension of an Array

The number N of coordinates in the domain of the function arr is called the dimension (or sometimes rank ) of the array. Thus, every array has a dimension (note the singular); it is not correct to refer to the dimension s of an array (note the plural). An array of dimension 1 is called a one-dimensional array , an array of dimension 2 is called a two-dimensional array , and so on.

3.4.3 Size of an Array

Along with a dimension, every array has a size . For instance, the one-dimensional array:

 arr:{0,1,...,5}  T 

has size 6. The two-dimensional array:

 arr:{0,1,...,5}{0,1,...,8}  T 

has size 6 ·9. The three-dimensional array:

 arr:{0,1,...,5}{0,1,...,8}{0,1}  T 

has size 6 ·9 ·2.

3.4.4 Arrays in VB.NET

In VB.NET, all arrays have lower bound 0. This is a change from earlier versions of VB, where we could choose the lower bound of an array.

The following examples show various ways to declare a one-dimensional array:

 ' Implicit constructor: No initial size and no initialization Dim Days() As Integer ' Explicit constructor: No initial size and no initialization Dim Days() As Integer = New Integer() {} ' Implicit constructor: Initial size but no initialization Dim Days(6) As Integer ' Explicit constructor: Initial size but no initialization Dim Days() As Integer = New Integer(6) {} ' Implicit constructor: Initial size implied by initialization Dim Days() As Integer = {1, 2, 3, 4, 5, 6, 7} ' Explicit constructor, Initial size and initialization Dim Days() As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7} 

Note that an array declaration can:

  • Call the array's constructor implicitly or explicitly. (The constructor is the function that VB.NET uses to create the array.)

  • Specify an initial size for each dimension or leave the initial size unspecified.

  • Initialize the elements of the array or not.

It is important to note that in the declaration:

 Dim ArrayName(X) As ArrayType 

the number X is the upper bound of the array. Thus, the array elements are ArrayName(0) through ArrayName(X), and the array has X+1 elements.

Multidimensional arrays are declared similarly. For instance, the following example declares and initializes a two-dimensional array:

 Dim X(,) As Integer = {{1, 2, 3}, {4, 5, 6}} 

and the following code displays the contents of the array:

 Debug.Write(X(0, 0)) Debug.Write(X(0, 1)) Debug.Writeline(X(0, 2)) Debug.Write(X(1, 0)) Debug.Write(X(1, 1)) Debug.Write(X(1, 2)) 123 456 

In VB.NET, all arrays are dynamic; there is no such thing as a fixed-size array. The declared size should be thought of simply as the initial size of the array, which is subject to change using the ReDim statement. Note, however, that the dimension of an array cannot be changed.

Moreover, unlike with VB 6, the ReDim statement cannot be used for array declaration, but can be used only for array redimensioning. All arrays must be declared initially using a Dim (or equivalent) statement.

3.4.4.1 Redimensioning arrays

The ReDim statement is used to change the size of an array. This is referred to as redimensioning ” a term no doubt invented by someone who didn't know the difference between the dimension of an array and the size of an array! In any case, redimensioning changes the size of the array, not its dimension. In fact, as we have already mentioned, the dimension of an array cannot be changed.

The UBound function returns the upper limit of an array coordinate. Its syntax is:

 UBound(   MyArray   ,   CoordinateIndex   ) 

where CoordinateIndex is the index of the coordinate for which we want the upper bound.

Here is an example of array redimensioning:

 Dim MyArray(10, 10) As Integer Msgbox(UBound(MyArray, 2))               ' Displays 10 ReDim MyArray(15, 20) Msgbox(UBound(MyArray, 2))               ' Displays 20 

When an array is redimensioned using the ReDim statement without qualification, all data in the array is lost; that is, the array is reinitialized. However, the Preserve keyword, when used with ReDim , redimensions the array while retaining all current values. Note that when using the Preserve keyword, only the last coordinate of an array can be changed. Thus, referring to the array defined earlier, the following code generates an error:

 ReDim Preserve MyArray(50, 20) 

You will probably not be surprised to learn that redimensioning an array is a time- intensive process. Hence, when redimensioning, we face the ubiquitous dichotomy between saving space and saving time. For instance, consider the code segment used to populate an array:

 Dim MyArray(100) As Integer Dim i As Integer, iNext As Integer iNext = 0 Do While (Some condition)     If (some condition here) Then         ' Add element to array         If ubound(MyArray) < iNext Then             ReDim Preserve MyArray(iNext + 100)         End If         MyArray(iNext) = (whatever)         iNext = iNext + 1     End If Loop 

The key issue here is to decide how much to increase the size of the array each time resizing is necessary. If we want to avoid using any extra space, we could increase the size of the array by 1 each time:

 ReDim Preserve MyArray(iNext + 1) 

But this would be very inefficient. Alternatively, we could kick up the size by 1,000:

 ReDim Preserve MyArray(iNext + 1000) 

But this uses a lot of extra space. Sometimes experimentation is required to find the right compromise between saving space and saving time.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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