illustrates the use of ArrayList properties and methods : ArrayList « Collections Data Structure « C# / C Sharp






illustrates the use of ArrayList properties and methods

illustrates the use of ArrayList properties and methods
  
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example11_2.cs illustrates the use of ArrayList properties
  and methods
*/

using System;
using System.Collections;

public class Example11_2
{

  // the DisplayArrayList() method displays the elements in the
  // ArrayList that is supplied as a parameter
  public static void DisplayArrayList(
    string arrayListName, ArrayList myArrayList
  )
  {
    for (int counter = 0; counter < myArrayList.Count; counter++)
    {
      Console.WriteLine(arrayListName + "[" + counter + "] = " +
        myArrayList[counter]);
    }
  }

  public static void Main()
  {

    // create an ArrayList object
    ArrayList myArrayList = new ArrayList();

    // set and display the Capacity property
    myArrayList.Capacity = 10;
    Console.WriteLine("myArrayList.Capacity = " +
      myArrayList.Capacity);

    // display the IsFixedSize and IsReadOnly properties
    Console.WriteLine("myArrayList.IsFixedSize = " +
      myArrayList.IsFixedSize);
    Console.WriteLine("myArrayList.IsReadOnly = " +
      myArrayList.IsReadOnly);

    // add eight string elements to myArrayList using the Add(),
    // Insert(), AddRange(), and InsertRange() methods
    Console.WriteLine("Adding eight string elements to myArrayList");
    myArrayList.Add("This");
    myArrayList.Insert(1, "is");
    string[] myStringArray = {"a", "test"};
    myArrayList.AddRange(myStringArray);
    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.InsertRange(myArrayList.Count, anotherStringArray);

    // display the elements in myArrayList using the
    // DisplayArrayList() method defined earlier
    DisplayArrayList("myArrayList", myArrayList);

    // use the SetRange() method to copy the elements from
    // anotherStringArray to myArrayList, starting at index 0
    Console.WriteLine("Using the SetRange() method to copy the\n" +
      "elements from anotherStringArray to myArrayList,\n" +
      "starting at index 0");
    myArrayList.SetRange(0, anotherStringArray);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Contains() method to determine if the string "text"
    // is in the ArrayList; if it is, then use the IndexOf() and
    // LastIndexOf() methods to display the first and last occurrence
    if (myArrayList.Contains("text"))
    {
      int index = myArrayList.IndexOf("text");
      Console.WriteLine("myArrayList does contain the word 'text'");
      Console.WriteLine("'text' first occurs at index " + index);
      index = myArrayList.LastIndexOf("text");
      Console.WriteLine("'text' last occurs at index " + index);
    }

    // remove element 0, first "text" element, and two
    // elements starting at index 3
    Console.WriteLine("Removing elements from myArrayList");
    myArrayList.RemoveAt(0);
    myArrayList.Remove("text");
    myArrayList.RemoveRange(3, 2);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Sort() method to sort myArrayList
    Console.WriteLine("Using the Sort() method to sort myArrayList");
    myArrayList.Sort();
    DisplayArrayList("myArrayList", myArrayList);

    // use the BinarySearch() method to search myArrayList
    Console.WriteLine("Using the BinarySearch() method to search myArrayList");
    int index2 = myArrayList.BinarySearch("some");
    Console.WriteLine("Found 'some' at index " + index2);

    // use the Reverse() method to reverse myArrayList
    Console.WriteLine("Using the Reverse() method");
    myArrayList.Reverse();
    DisplayArrayList("myArrayList", myArrayList);

    // use the TrimToSize() method to reduce the capacity of
    // myArrayList to the actual number of elements in myArrayList
    Console.WriteLine("Using the TrimToSize() method to reduce the\n" +
      "capacity of myArrayList");
    myArrayList.TrimToSize();
    Console.WriteLine("myArrayList.Capacity = " +
      myArrayList.Capacity);

    // use the GetRange() method to get a range of elements
    // from myArrayList
    Console.WriteLine("Using the GetRange() method to get two\n" +
      "elements from myArrayList, starting at index 1");
    ArrayList anotherArrayList = myArrayList.GetRange(1, 2);
    DisplayArrayList("anotherArrayList", anotherArrayList);

    // get an enumerator using the GetEnumerator() method
    // and use it to read the elements in myArrayList
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }

    // use the Reset() method and access the first row again using MoveNext()
    Console.WriteLine("Using the Reset() method and accessing\n" +
      " the first row again using MoveNext()");
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);

    // Use a foreach statement to read the contents of myArrayList
    Console.WriteLine("Using a foreach statement to read the contents of myArrayList");
    foreach (string myString in myArrayList)
    {
      System.Console.WriteLine("myString = " + myString);
    }

  }

}


           
         
    
  








Related examples in the same category

1.Add items to ArrayList and use foreach loop to check
2.Add array to ArrayList
3.Remove range from ArrayList
4.Clone an ArrayList
5.CopyTo, ToArray(), ToArray(typeof(String))
6.Using foreach to loop through ArrayList and use Indexer of ArrayList
7.Check InvalidCastException when using foreach loop with ArrayList
8.Binary Search an ArrayList
9.Inserting into an ArrayList by index
10.Checks time needed for list operations using an ArrayList implementationChecks time needed for list operations using an ArrayList implementation
11.ArrayList Demo: hold classArrayList Demo: hold class
12.illustrates the use of an ArrayList that contains objects of the Car classillustrates the use of an ArrayList that contains objects of the Car class
13.illustrates the use of ArrayLists 2illustrates the use of ArrayLists 2
14.Convert an ArrayList into an arrayConvert an ArrayList into an array
15.Sort and search an ArrayListSort and search an ArrayList
16.Demonstrate ArrayListDemonstrate ArrayList
17.Search for the first occurrence of the duplicated value
18.Search for the first occurrence of the duplicated value in the last section of the ArrayList
19.Search for the first occurrence of the duplicated value in a section of the ArrayList
20.Search for the first occurrence of the duplicated value in a small section at the end of the ArrayList
21.Convert a ArrayList object to a array
22.Represents a loose abstraction of an arraylist, wheres the buffer array is available for public access.