Creating Dynamic Lists


As you saw in the "Copying an Array" section, it isn't easy to maintain a dynamic list in which the array can potentially grow or shrink. It involves creating new arrays and then copying the elements from the old array into the new array. However, there are a set of classes in .NET that make managing dynamic lists easier. One of these classes is System.Collections.ArrayList .

To create an ArrayList:

  1. Type System.Collections.ArrayList .

  2. Type the name of the variable to hold the arraylist, for example: names .

  3. Type = .

  4. Type new System.Collections.ArrayList() .

  5. Type a semicolon ; .

  6. Type names.Add

  7. Type an open parenthesis ( .

  8. Type the value to add to the list, for example: "William" .

  9. Type a close parenthesis ) .

  10. Type a semicolon ; ( Figure 9.50 ).

    Figure 9.50 ArrayLists are dynamic arrays. You can keep adding items with Add and the ArrayList will grow as needed. Internally ArrayList stores information in an array. When there is no room in the array, it creates a new array and copies the elements from the old array into the new array, thus giving you the illusion that it's a dynamic array. (Knowing that sort of takes the fun out of it, doesn't it?)
     void Task() {    System.Collections.ArrayList systemUsers =    new System.Collections.ArrayList();    systemUsers.Add("Batman");    systemUsers.Add("Robin");    systemUsers.Add("Cat Woman"); } 

graphics/tick.gif Tips

  • The array list is essentially a dynamic array. Internally it builds an array of 16 elements. If you add more than 16 elements, it doubles the size of the array and copies the elements from the old array into the new array. Every time you pass the limit, it needs to create a new array. It makes more sense to start with a large capacity if you know approximately how many items you wish to add ( Figure 9.51 ).

    Figure 9.51 The capacity doesn't limit how many items you can have. It just pre- allocates space for that many items. If you go over the capacity, then ArrayList will double its capacity, but that could hurt performance.
     //using System.Collections; ArrayList systemUsers = new  ArrayList(500)  ; 
  • You enumerate through the items of the array list using an indexer ( Figure 9.52 ).

    Figure 9.52 The ArrayList class lets you navigate through the items of the array using an index, just like an array. One difference, however, is that ArrayLists have a Count property instead of the Length property that arrays use to report the number of elements.
     void Task2() {    System.Collections.ArrayList    systemUsers =    new System.Collections.ArrayList(500);    systemUsers.Add("Batman");    systemUsers.Add("Robin");    systemUsers.Add("Cat Woman");  for(int count = 0; count <   systemUsers.Count; count++)   {   string name =   (string)systemUsers[count];   }  } 
  • You can also remove items from the array list either specifying the object with the Remove function or specifying the index number with the RemoveAt function ( Figure 9.53 ).

    Figure 9.53 You can remove based on a value, or based on an index. After the two remove functions, the only element left in the list is Cat Woman.
     void Task2() {    System.Collections.ArrayList systemUsers =    new System.Collections.ArrayList(500);    systemUsers.Add("Batman");    systemUsers.Add("Robin");    systemUsers.Add("Cat Woman");  systemUsers.Remove("Robin");   systemUsers.RemoveAt(0);  } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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