Section 16.2. Array Lists

   

16.2 Array Lists

Imagine that your program asks the user for input or gathers input from a web site. As it finds objects (strings, books, values, etc.), you would like to add them to an array, but you have no idea how many objects you'll collect in any given session.

It is difficult to use an array for such a purpose because you must declare the size of an Array object at compile time. If you try to add more objects than you've allocated memory for, the Array class throws an exception. If you do not know in advance how many objects your array will be required to hold, you run the risk of declaring either too small an array (and running out of room) or too large an array (and wasting memory).

The .NET Framework provides a class designed for just this situation. The ArrayList class is an array whose size is dynamically increased as required. The ArrayList class provides many useful methods and properties. Table 16-3 shows a few of the most important.

Table 16-3. ArrayList members

Method or property

Purpose

Capacity

Property containing the number of elements the array can currently hold

Count

Property to return the number of elements currently in the array

Item()

Method that gets or sets the element at the specified index. This is the indexer for the ArrayList class

Add()

Method to add an object to the ArrayList

Clear()

Method that removes all elements from the ArrayList

GetEnumerator()

Method that returns an enumerator to iterate an ArrayList

Insert()

Method that inserts an element into ArrayList

RemoveAt()

Method that removes the element at the specified index

Reverse()

Method that reverses the order of elements in the ArrayList

Sort ()

Method that alphabetically sorts the ArrayList

ToArray()

Method that copies the elements of the ArrayList to a new array

When you create an ArrayList, you do not define how many objects it will contain. You add to the ArrayList using the Add() method, and the list takes care of its own internal bookkeeping, as illustrated in Example 16-2.

Example 16-2. Using an ArrayList
 using System; using System.Collections; namespace ArrayListDemo {     // a class to hold in the array list     public class Employee     {         private int empID;         public Employee(int empID)         {             this.empID = empID;         }         public override  string ToString()         {             return empID.ToString();         }         public int EmpID         {             get { return empID; }             set { empID = value; }         }     }     class Tester     {         public void Run()         {             ArrayList empArray = new ArrayList();             ArrayList intArray = new ArrayList();             // populate the arraylists             for (int i = 0;i<5;i++)             {                 empArray.Add(new Employee(i+100));                 intArray.Add(i*5);             }                       // print each member of the array             foreach (int i in intArray)             {                 Console.Write("{0} ", i.ToString());             }             Console.WriteLine("\n");             // print each employee             foreach(Employee e in empArray)             {                 Console.Write("{0} ", e.ToString());             }             Console.WriteLine("\n");             Console.WriteLine("empArray.Capacity: {0}",                 empArray.Capacity);         }         [STAThread]         static void Main()         {             Tester t = new Tester();             t.Run();         }     } }  Output:  0 5 10 15 20 100 101 102 103 104 empArray.Capacity: 16 

Suppose you're defining two ArrayList objects, empArray to hold Employee objects and intArray to hold integers:

 ArrayList empArray = new ArrayList(); ArrayList intArray = new ArrayList(); 

Each ArrayList object has a property, Capacity, which is the number of elements the ArrayList is capable of storing:

 public int Capacity {virtual get; virtual set; } 

The default capacity for the ArrayList class is 16. You are free to set a different starting capacity for your ArrayList, but typically there is no need for you ever to do so.

Add elements to the ArrayList with the Add() method:

 empArray.Add(new Employee(i+100)); intArray.Add(i*5); 

When you add the 17th element, the capacity is automatically doubled to 32. If you change the for loop to:

 for (int i = 0;i<17;i++) 

the output looks like this:

 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 empArray.Capacity: 32 

Similarly, if you added a 33 rd element, the capacity is doubled to 64. The 65 th element increases the capacity to 128, the 129th element increases it to 256, and so forth.

   


Learning C#
Learning C# 3.0
ISBN: 0596521065
EAN: 2147483647
Year: 2005
Pages: 178

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