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