Section 8.13. Changing the Size of an Array at Execution Time: Using the ReDim Statement


8.13. Changing the Size of an Array at Execution Time: Using the ReDim Statement

The number of elements in an array can be changed at execution time. For example, when an array is used to store information about all the employees in a company, the size of the array would change when the company hires a new employee or when an employee leaves the company. The ReDim statement enables you to dynamically change the array size, but not the type of the array elements, nor the number of dimensions in the array. Figure 8.21 demonstrates the ReDim statement.

Figure 8.21. Using ReDim statements to change the array size.

  1  ' Fig. 8.21: ReDimTest.vb  2  ' Resize an array using the ReDim statement.  3  Module ReDimTest  4     Sub Main()  5        ' create and initialize a 5-element array  6        Dim array As Integer() = {1, 2, 3, 4, 5}  7        Dim arrayCopy As Integer() = array  8  9        ' display array length and the elements in array 10        Console.Write("The original array has {0} elements: ", _ 11           array.Length) 12        DisplayArray(array) 13 14        ' change the size of the array without the Preserve keyword 15        ReDim array(6)                                              16 17        ' display new array length and the elements in array 18        Console.Write("New array (without Preserve) has {0} elements: ", _ 19           array.Length) 20        DisplayArray(array) 21 22        ' change the size of the array with the Preserve keyword 23        ReDim Preserve arrayCopy(6)                              24        arrayCopy(6) = 7 ' assign 7 to array element 6           25 26        ' display new array length and the elements in array 27        Console.Write("New array (with Preserve) has {0} elements: ", _ 28           arrayCopy.Length) 29        DisplayArray(arrayCopy) 30     End Sub ' Main 31 32     ' display array elements 33     Sub DisplayArray(ByVal array As Integer()) 34        For Each number As Integer In array 35           Console.Write("{0} ", number) 36        Next 37 38        Console.WriteLine() 39      End Sub ' DisplayArray 40   End Module ' ReDimTest 

[View full width]

The original array has 5 elements: 1 2 3 4 5 New array (without Preserve) has 7 elements: 0 0 0 0 0 0 0 New array (with Preserve) has 7 elements: 1 2 3 4 5 0 7



Line 6 creates and initializes a five-element array array. Line 7 creates a copy of the array named arrayCopy. Lines 1012 display the size and elements of the original array. Line 15 uses a ReDim statement to change the upper bound of array to 6, so that the array now contains seven elements. The ReDim statement contains keyword ReDim, followed by the name of the array to be resized and the new upper bound in parentheses. The output of Fig. 8.21 shows that after the ReDim statement is executed, the size of the array is changed to 7 and the value of each element is reinitialized to the default value of the type of the array element (i.e., 0 for Integers). To save the original data stored in an array, follow the ReDim keyword with the optional Preserve keyword. Line 23 uses Preserve in the ReDim statement to indicate that the existing array elements are to be preserved in the now larger array after the array is resized. If the new array is smaller than the original array, the existing elements that are outside the bounds of the new array are discarded. If the new array is larger than the original array, all the existing elements are preserved in the now larger array, and the extra elements are initialized to the default value of the type of the array element. For example, after line 23 is executed, the value of arrayCopy(5) is 0. Line 24 assigns the value 7 to arrayCopy(6), so that the now larger array arrayCopy contains elements 1, 2, 3, 4, 5, 0 and 7.

In Chapter 26, Collections, we introduce class ArrayList from the System.Collections namespace. An ArrayList is a dynamically resizable array-like data structure that mimics the functionality of conventional arrays. Additional ArrayList capabilities include inserting elements, searching for elements, removing elements and sorting elements.



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