illustrates how to use array properties and methods : Array : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array »

 

illustrates how to use array properties and methods









    

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example10_5.cs illustrates how to use array properties
  and methods
*/

using System;

public class Example10_5
{

  public static void Main()
  {

    // use the Sort() method to sort the elements in an int array
    int[] intArray = {5, 2, 3, 1, 6, 9, 7, 14, 25};
    Array.Sort(intArray);  // sort the elements
    Console.WriteLine("Sorted intArray:");
    for (int counter = 0; counter < intArray.Length; counter++)
    {
      Console.WriteLine("intArray[" + counter + "] = " +
        intArray[counter]);
    }

    // use the Sort() method to sort the elements in a string array
    string[] stringArray = {"this", "is", "a", "test", "abc123", "abc345"};
    Array.Sort(stringArray);  // sort the elements
    Console.WriteLine("Sorted stringArray:");
    for (int counter = 0; counter < stringArray.Length; counter++)
    {
      Console.WriteLine("stringArray[" + counter + "] = " +
        stringArray[counter]);
    }

    // use the Sort() method to sort the elements in a char array
    char[] charArray = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
    Array.Sort(charArray);  // sort the elements
    Console.WriteLine("Sorted charArray:");
    for (int counter = 0; counter < charArray.Length; counter++)
    {
      Console.WriteLine("charArray[" + counter + "] = " +
        charArray[counter]);
    }

    // use the BinarySearch() method to search intArray for the number 5
    int index = Array.BinarySearch(intArray, 5);
    Console.WriteLine("Array.BinarySearch(intArray, 5) = " + index);

    // use the BinarySearch() method to search intArray for the number 4
    // (this number doesn't exist in intArray, and therefore BinarySearch()
    // returns a negative value)
    index = Array.BinarySearch(intArray, 4);
    Console.WriteLine("Array.BinarySearch(intArray, 4) = " + index);

    // use the BinarySearch() method to search stringArray for "abc345"
    index = Array.BinarySearch(stringArray, "abc345");
    Console.WriteLine("Array.BinarySearch(stringArray, \"abc345\") = " + index);

    // use the BinarySearch() method to search charArray for 'o'
    index = Array.BinarySearch(charArray, 'o');
    Console.WriteLine("Array.BinarySearch(charArray, 'o') = " + index);

    // use the Reverse() method to reverse the elements in intArray
    Array.Reverse(intArray);
    Console.WriteLine("Reversed intArray:");
    for (int counter = 0; counter < intArray.Length; counter++)
    {
      Console.WriteLine("intArray[" + counter + "] = " +
        intArray[counter]);
    }

    // use the Reverse() method to reverse the elements in stringArray
    Array.Reverse(stringArray);
    Console.WriteLine("Reversed stringArray:");
    for (int counter = 0; counter < stringArray.Length; counter++)
    {
      Console.WriteLine("stringArray[" + counter + "] = " +
        stringArray[counter]);
    }

    // use the Reverse() method to reverse the elements in charArray
    Array.Reverse(charArray);
    Console.WriteLine("Reversed charArray:");
    for (int counter = 0; counter < charArray.Length; counter++)
    {
      Console.WriteLine("charArray[" + counter + "] = " +
        charArray[counter]);
    }

    // create another array of int values named intArray2
    int[] intArray2 = {1, 2, 1, 3};
    Console.WriteLine("intArray2:");
    for (int counter = 0; counter < intArray2.Length; counter++)
    {
      Console.WriteLine("intArray2[" + counter + "] = " +
        intArray2[counter]);
    }

    // use the IndexOf() and LastIndexOf() methods to find the value 1
    // in intArray2
    index = Array.IndexOf(intArray2, 1);
    Console.WriteLine("Array.IndexOf(intArray2, 1) = " + index);
    index = Array.LastIndexOf(intArray2, 1);
    Console.WriteLine("Array.LastIndexOf(intArray2, 1) = " + index);

    // create another array of strings named stringArray2
    string[] stringArray2 = {"Hello", "to", "everyone", "Hello", "all"};
    Console.WriteLine("stringArray2:");
    for (int counter = 0; counter < stringArray2.Length; counter++)
    {
      Console.WriteLine("stringArray2[" + counter + "] = " +
        stringArray2[counter]);
    }

    // use the IndexOf() and LastIndexOf() methods to find the string "Hello"
    // in intArray2
    index = Array.IndexOf(stringArray2, "Hello");
    Console.WriteLine("Array.IndexOf(stringArray2, \"Hello\") = " + index);
    index = Array.LastIndexOf(stringArray2, "Hello");
    Console.WriteLine("Array.LastIndexOf(stringArray2, \"Hello\") = " + index);

  }

}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array