14.3 System.ArrayVB.NET implements arrays with the class System.Array. The Array class has a number of useful methods. Table 14-1 shows a few of the more important methods and properties of the System.Array class. Table 14-1. Useful methods and properties of System.Array
The Array class's shared methods, Reverse( ) and Sort( ), make manipulation of the objects within the array very easy. Note, however, that to reverse or sort the elements of the array, they must be of a type that implements the IComparable interface, described in Chapter 13. The .NET Framework includes the String class, which does implement this interface, so we'll demonstrate both Reverse( ) and Sort( ) with Strings. The complete listing is shown in Example 14-9, followed by the output and analysis. Example 14-9. Sort( ) and Reverse( ) methods of ArrayOption Strict On Imports System Namespace ReverseAndSort Class Tester Public Shared Sub DisplayArray(ByVal theArray( ) As Object) Dim obj As Object For Each obj In theArray Console.WriteLine("Value: {0}", obj) Next obj Console.WriteLine(ControlChars.Lf) End Sub 'DisplayArray Public Sub Run( ) Dim myArray As [String]( ) = {"Who", "is", "John", "Galt"} Console.WriteLine("Display myArray...") DisplayArray(myArray) Console.WriteLine("Reverse and display myArray...") Array.Reverse(myArray) DisplayArray(myArray) Dim myOtherArray As [String]( ) = _ {"We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident"} Console.WriteLine("Display myOtherArray...") DisplayArray(myOtherArray) Console.WriteLine("Sort and display myOtherArray...") Array.Sort(myOtherArray) DisplayArray(myOtherArray) End Sub 'Run Public Shared Sub Main( ) Dim t As New Tester( ) t.Run( ) End Sub 'Main End Class 'Tester End Namespace 'ReverseAndSort Output: Display myArray... Value: Who Value: is Value: John Value: Galt Reverse and display myArray... Value: Galt Value: John Value: is Value: Who Display myOtherArray... Value: We Value: Hold Value: These Value: Truths Value: To Value: Be Value: Self Value: Evident Sort and display myOtherArray... Value: Be Value: Evident Value: Hold Value: Self Value: These Value: To Value: Truths Value: We Example 14-9 begins by creating myArray, an array of strings, containing the words: "Who", "is", "John", "Galt" This array is displayed and then passed to the Array.Reverse( ) method, where it is displayed again to see that the array itself has been reversed : Value: Galt Value: John Value: is Value: Who Similarly, the example creates a second array, myOtherArray, containing the words: "We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident", which is passed to the Array.Sort( ) method. Then Array.Sort( ) happily sorts them alphabetically : Value: Be Value: Evident Value: Hold Value: Self Value: These Value: To Value: Truths Value: We The method to display the strings has been made somewhat generic by declaring the type passed in to be an array of objects: Public Shared Sub DisplayArray(ByVal theArray( ) As Object) The DisplayArray( ) method iterates through the array of objects, passing each to WriteLine( ). Since WriteLine( ) calls ToString( ) on objects, and since every object (including String) supports ToString( ), declaring the temporary variable obj to be of type Object works very well. Using objects has the advantage that you can reuse your DisplayArray( ) method with arrays of other types of objects, once you know how to implement the IComparable interface (shown in the next chapter). |