Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

488 Chapter 11 STORING DATA IN COLLECTIONS

the elements in the array Names, call the Reverse method passing the Names array as argument. The reversed array must be assigned to another array, ReverseNames in our example:

Dim Names(99) As String

{ statements to populate array Names } Dim ReverseNames() As String

ReverseNames = System.Array.Reverse(Names)

After the execution of the last statement, the ReverseNames array contains the same elements as the Names array in reverse order. Notice that the ReverseNames array need not be dimensioned when it’s declared; the Reverse method will dimension the array accordingly.

The Copy and CopyTo methods copy the elements of an array (or segment of an array) to another array. The Copy method copies a range of elements from one array to another, and its syntax is

System.Array.Copy(sourceArray, destinationArray, count)

sourceArray and destinationArray are the names of the two arrays, and count is the number of elements to be copied. The copying process starts with the first element of the source array and ends after the first count elements have been copied. If count is less than the length of the source array, or it exceeds the length of the second array, an exception will be thrown.

Warning Both the Copy and CopyTo methods work with one-dimensional arrays only.

Another form of the Copy method allows you to specify the range of elements in the source array to be copied and a range in the destination array where these elements will be copied. The syntax of this form of the method is

System.Array.Copy(sourceArray, sourceStart,_ destinationArray, destinationStart, count)

This method copies count elements from the source array starting at location sourceStart and places them in the destination array starting at location destinationStart. All indices must be valid, and there should be count elements after the sourceStart index in the source array, as well as count elements after the destinationStart in the destination array. If not, a runtime error will be generated.

The CopyTo method is similar, but it doesn’t require the name of the source array. It copies the elements of the array to which it’s applied into the destination array:

System.Array.CopyTo(destinationArray, sourceStart)

Array Limitations

VB.NET arrays are more flexible than ever. The most demanding tasks programmers had to perform with arrays are now implemented as methods of the Array class. However, arrays aren’t perfect for all types of data storage. One problem with arrays is that they’re not dynamic structures. Resizing the array is a time-consuming operation. There is no simple method to insert additional elements or delete elements anywhere in the array. To remove the third item from an array, you must move up all the elements after the third one by one position. The element array(3) must become array(2), array(4) must become array(3), and so on. You can easily implement this technique with a loop, but what good is it going to be with a large array?

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com