4.5 Using ArrayLists

 <  Day Day Up  >  

4.5 Using ArrayList s

You want to use an array but want the array to grow if elements go past the initial array size .


Technique

Declare an ArrayList variable and instantiate an object of that type. You can optionally use two overloaded constructors. One constructor allows you to pass an integer to specify the initial size or capacity of the internal array, and the second overloaded constructor allows you to pass in a collection object whose values are copied into the new ArrayList object:

 
 static void CreateArrayLists() {     ArrayList al1 = new ArrayList();     ArrayList al2 = new ArrayList( 10 );     int[] intArr = new int[10];     ArrayList al3 = new ArrayList( intArr ); } 

To adjust the size of the ArrayList , use the Capacity property. This value must be greater than the value of the Count property. In other words, you can make the array larger but not smaller:

 
 ArrayList myArrayList = new ArrayList(); myArrayList.Capacity = 10; 

To add objects to the end of the ArrayList , use the Add or AddRange method. Add allows you to add a single object to the array, whereas AddRange allows you to add an entire collection. If the number of elements exceeds the size of the array, the ArrayList class will allocate additional memory to make room:

 
 int[] intArr = {2, 3, 4, 5}; myArrayList.Add( 1 ); myArrayList.AddRange( intArr ); 

You can also use Remove , which removes a specified object from the array; RemoveAt , which removes an object given an index into the array; and RemoveRange to remove a specified number of objects beginning at a certain index into the array:

 
 myArrayList.Remove( 1 ); myArrayList.RemoveRange( 1, 2 ); 

To insert elements in the middle of the array, use the Insert method, which inserts an object at a specified index within the array, and InsertRange , which inserts the contents of a collection at a specified index within the array:

 
 myArrayList.Insert( 1, 3 ); myArrayList.InsertRange( 0, intArr ); 

Comments

In the previous recipe, you saw how to initialize an array of a certain data type and set the values of that array using indexers. These arrays were fixed in size. If you attempt to set a value using an index value that is greater than the size of the array, an IndexOutOfRangeException is thrown and the application prematurely exits. The ArrayList , on the other hand, allows you to increase the size of the internal array to make room for more objects.

One important thing to note is that although the ArrayList also has the ability to get or set values using an indexer, as with a regular array the index value cannot be greater than the size of the internal array. You can find the size of the internal array by accessing the Count property. If you need to set a value at an index that is greater than the Count property, set the Capacity property to a larger value. Internally, the ArrayList does a few things when the Capacity property is changed. First of all, a new array is created using the new value of the Capacity property. The values of the original array are then copied over to the new array. Finally, because the original array is no longer needed, it is marked for removal.

One major difference between the Array and ArrayList class is that the ArrayList class can only work with generic objects. An Array allows you to create an array using a specified data type. Therefore, when accessing an element from the ArrayList , you might have to perform a data-type conversion to work with that value, as shown in the following code:

 
 static void Main(string[] args) {     ArrayList al = new ArrayList();     for( int i = 0; i < 10; i++ )     {         al[i] = i;     }     // compilation error     int fifthElement = al[5];     // compiles fine     int fourthElement = (int) al[4]; } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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