Using Array Lists

Unlike Visual Basic .NET, where you can use the ReDim statement to re-dimension an array, arrays are of a fixed size in C#. There are times when you might want to use variable-length arrays in your code, however, as when the user specifies at runtime how many items an array should have, or when you're opening various unknown databases full of records. The ArrayList class lets you add or remove elements to an array at runtime, thus creating dynamic-length arrays. You can see the significant public properties of ArrayList objects in Table 6.5, and the significant public methods of these objects in Table 6.6.

Table 6.5. Significant Public Properties of ArrayList Objects

PROPERTY

PURPOSE

Capacity

Returns or sets the number of elements that the array list can contain.

Count

Returns the number of elements in the array list.

IsFixedSize

Returns true if the array list has a fixed size.

IsReadOnly

Returns true if the array list is read-only.

IsSynchronized

Returns true if access to the array list is thread-safe.

Item

Returns or sets the element at the given index.

Table 6.6. Significant Public Methods of ArrayList Objects

METHOD

PURPOSE

Add

Adds an object to the end of the array list.

AddRange

Adds the elements of a collection to the end of the array list.

BinarySearch

Uses a binary search algorithm to locate a specific element in the sorted array list.

Clear

Removes all elements.

Contains

Determines whether an element is in the array list.

CopyTo

Copies the array list to a one-dimensional array.

GetEnumerator

Returns an enumerator that can iterate through the array list.

GetRange

Returns an array list that represents a subset of the elements in the source array list.

IndexOf

Returns the zero-based index of the first occurrence of a value.

Insert

Inserts an element into the array list.

InsertRange

Inserts the elements of a collection into the array list at the given index.

LastIndexOf

Returns the zero-based index of the last occurrence of a value.

Remove

Removes the first occurrence of a specific object.

RemoveAt

Removes the element at the given index of the array list.

RemoveRange

Removes a range of elements from the array list.

Reverse

Reverses the order of the elements.

SetRange

Copies the elements of a collection over a range of elements in the array list.

Sort

Sorts the elements in the array list.

ToArray

Copies the elements of the array list to a new array.

TrimToSize

Sets the capacity to the actual number of elements.

Using array lists, you can use the Add method to add new elements, and the Remove and RemoveAt methods to remove elements. You can access elements with the [] operator as you can with standard arrays. As an example, we'll create a new array list and add a few elements to it, and then loop over it with a for loop (using the Count property to determine how many elements are in the array list). The elements are displayed this way:

 
 ArrayList arrayList = new ArrayList(); arrayList.Add("Now"); arrayList.Add("is"); arrayList.Add("the"); arrayList.Add("time"); for (int loopIndex = 0; loopIndex < arrayList.Count; loopIndex++) {   System.Console.Write("{0} ", arrayList[loopIndex]); } 

You can also loop over the elements in an array list with foreach , as you see in ch06_06.cs, Listing 6.6.

Listing 6.6 Creating an Array List (ch06_06.cs)
 using System.Collections; public class ch06_06 {   public static void Main()   {     ArrayList arrayList = new ArrayList();     arrayList.Add("Now");     arrayList.Add("is");     arrayList.Add("the");     arrayList.Add("time");     for (int loopIndex = 0; loopIndex < arrayList.Count; loopIndex++)     {       System.Console.Write("{0} ", arrayList[loopIndex]);     }     System.Console.WriteLine();  foreach(object text in arrayList)   {   System.Console.Write("{0} ", text);   }  } } 

Here's what you see when you run ch06_06:

 
 C:\>ch06_06 Now is the time Now is the time 

Note also that, unlike standard arrays, array lists can store data of different types:

 
 arrayList.Add("No worries."); arrayList.Add(5); 


Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net