16.2 Array ListsImagine 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.
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 ArrayListusing 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; }
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 33rd element, the capacity is doubled to 64. The 65th element increases the capacity to 128, the 129th element increases it to 256, and so forth. |