15.3 System.ArrayC# implements arrays with the class System.Array. The Array class has a number of useful methods. Table 15-1 shows a few of the more important methods and properties of the System.Array class.
The Array class's static 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. (Chapter 16 describes the collection interfaces.)Chapter 15 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 15-9, followed by the output and analysis. Example 15-9. Reverse() and Sort() methods of Arrayusing System; namespace ReverseAndSort { class Tester { public static void DisplayArray(object[] theArray) { foreach (object obj in theArray) { Console.WriteLine("Value: {0}", obj); } Console.WriteLine("\n"); } public void Run() { String[] myArray = { "Who", "is", "John", "Galt" }; Console.WriteLine("Display myArray..."); DisplayArray(myArray); Console.WriteLine("Reverse and display myArray..."); Array.Reverse(myArray); DisplayArray(myArray); String[] myOtherArray = { "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); } [STAThread] static void Main() { Tester t = new Tester(); t.Run(); } } } 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 The example 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 static void DisplayArray(object[] theArray) The DisplayArray() method iterates through the array of objects, passing each to WriteLine(). foreach (object obj in theArray) { Console.WriteLine("Value: {0}", obj); } 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 Chapter 17). |