The Array class exposes additional methods, which are described briefly in this section. The Reverse method reverses the order of the elements in an array. The syntax of the Reverse method is the following:
reversedArray = System.Array.Reverse(arrayName)
Code language: PHP (php)
The Reverse method can’t be applied to an array and reverse its elements. Instead, it returns a new array with the elements of the array passed as an argument, only in reverse order.
The Copy and CopyTo methods copy the elements of an array (or segment of an array) to another array. The syntax of the Copy method is the following:
System.Array.Copy(sourceArray, destinationArray, count)
Code language: CSS (css)
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 exceeds the length of either array, an exception will be thrown.
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 in which these elements will be copied. The syntax of this form of the method is as follows:
System.Array.Copy(sourceArray, sourceStart, _
destinationArray, destinationStart, count)
Code language: CSS (css)
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, an exception will be thrown.
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, where sourceArray is a properly dimensioned and initialized array:
sourceArray.CopyTo(destinationArray, sourceStart)
Code language: CSS (css)
Finally, you can filter array elements by using the Filter() function, which is not a method of the Array class; it’s a VB function that acts on arrays. The Filter() function performs an element-by-element comparison and rejects the elements that don’t meet the user-specified criteria. The filtered elements are returned as a new array, while the original array remains intact. The syntax of the Filter() function is as follows:
filteredArray = Filter(source, match, include, compare)
Code language: PHP (php)
source is the array to be searched, and it must be a one-dimensional array of strings or objects. The match argument is the string to search for. Every element that includes the specified string is considered a match. The remaining arguments are optional: include is a True/False value indicating whether the method will return the elements that include (if True) or exclude (if False) the matching elements. The compare argument is a member of the CompareMethod enumeration:
It can be Binary (for binary or case-sensitive comparisons) or Text (for textual or case-insensitive comparisons). If no match is found, the method will return an empty array.
The following code segment filters out the strings that don’t contain the word visual from the words array:
Dim words() As String = {"Visual Basic", "Java", "Visual Studio"}
Dim selectedWords() As String
selectedWords = Filter(words, "visual", True, CompareMethod.Text)
Dim selword As String
Dim msg As String = ""
For Each selword In selectedWords
msg &= selword & vbCrLf
Next
MsgBox(msg)
Code language: PHP (php)
If you execute the preceding statements, the message box will display the following:
Visual Basic
Visual Studio
Change the last argument of the Filter function to CompareMethod.Binary, and no elements will be displayed. The method will return an empty array, because no element contains the search argument with the exact spelling as specified by the match argument.
Among the new array features introduced with version 3.5 of the Framework, I’ve singled out the FindAll method, which selects multiple elements and returns them as a new array. The FindAll method accepts two arguments: the array to be searched and a predicate, which is a pointer to a function that selects the desired elements. Here’s the syntax of the FindAll method:
System.Array.FindAll(array, match)
Code language: CSS (css)
The array argument must be strongly typed, and the match predicate must be a function that accepts as an argument a value of the same type as the array. The formal syntax of the FindAll method in the documentation is the following:
Array.FindAll(Of T)(array() As T, _
match As System.Predicate(Of T)) As T()
Code language: PHP (php)
It’s not as bad as it looks: T stands for Type. The FindAll method returns an array with elements of the T type. The array it accepts as an argument must also be of the same type. Finally, the predicate is a function that accepts as an argument an object of the T type.
Let’s say you have an array of integers and you want to select the ones that are positive and less than 100. Start by writing a function that accepts as an argument an integer value and returns it if it meets the specified criteria:
Private Function smallValue(ByVal v As Integer) As Integer
If v > 0 and v < 100 Then Return v
End Function
Code language: PHP (php)
Then call the FindAll method, passing a pointer to the smallValue() function with the AddressOf operator:
Dim values(999) As Integer
' statements to populate array
Dim selected() As Integer
Selected = System.Array.FindAll(values, AddressOf smallValue)
Code language: PHP (php)
The FindAll method is a shortcut to a loop that iterates through all the elements of the array and calls the same function to select or reject each element. The predicate is a regular function that can get as complicated as dictated by the needs of your application.
A similar method of the Array class, the TrueForAll method, applies a function to all the elements of the array and returns True if the function returns True for every element in the array. You can use this method to make sure that an array’s elements are of the same type, or that they all meet a specified requirement. The function you must provide should examine each element of the array and return True if it meets the criteria, as shown in the following sample function, which assumes that the elements are Rectangle objects and returns True if their area exceeds 1:
Private Function largeRectangle(ByVal R As Rectangle) As Boolean
If R.Width * R.Height > 1 Then
Return True
Else
Return False
End If
End Function
Code language: JavaScript (javascript)
The following statements show how you would use this function with the TrueForAll method. If True, then all the elements of the Rects array are large (their area exceeds 1):
Dim Rects(10000) As Rectangle
' statements to populate array
MsgBox(System.Array.TrueForAll(Rects, AddressOf largeRectangle))
Code language: PHP (php)
Array Limitations
As implemented in version 3.5 of the Framework, arrays are more flexible than ever. They’re very efficient, and 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. The most important shortcoming of arrays is that they’re not dynamic. Inserting or removing elements entails that all the following elements be moved up or down. The ArrayList collection is similar to the array, but it’s a dynamic structure. You can insert and remove elements to an ArrayList without having to worry about reordering the other elements in the collection. Arrays are the most efficient collection in the Framework, but when you need a dynamic structure for adding and removing elements in the course of an application, you should use an ArrayList object.