4.6 Choosing Between an Array and an ArrayList

 <  Day Day Up  >  

4.6 Choosing Between an Array and an ArrayList

When designing your application, you need to store data in a collection and you are unsure about whether to use a regular array or an ArrayList .


Technique

Use an ArrayList if

  • You need to add elements passed to the end of the array during runtime.

  • You want more methods that allow you to manipulate the ArrayList 's values.

  • You want synchronized access to elements but want the collection to take care of the details.

  • You want the ability to create read-only or fixed- size arrays.

Use a regular array if

  • You know exactly how many values you need to store in the array.

  • You need to use multiple dimensions.

  • You want the performance advantage that using an array provides.

Comments

It's tempting when choosing between an ArrayList and an Array to simply choose the ArrayList due to its greater support for object manipulation. However, you have to take a close look at how your data needs to be stored and how it will be accessed while also taking a look at the performance impact.

If the number of items to store is unbound or unknown, then using an ArrayList might be a good idea. One of the primary features of the ArrayList class is that it can grow internally as you place more items within it. Once you add an item at an index greater than the current capacity of the internal array, a new larger array is created, the contents of the original array are copied over, and that array is subsequently destroyed . By default, the internal array size has a capacity of 16 items. Once you add an item past that capacity, the array grows to support 16 additional items. By setting the Capacity property, you adjust the size of the internal array, but furthermore, you also adjust how many additional items are created when the array is expanded to accommodate additional items. For instance, if you set Capacity to 32, an additional 32 blocks of memory are created each time the Count of the internal array goes beyond the current Capacity . If you plan to use an ArrayList and you know that it will have to grow substantially, set the Capacity value to a larger number to decrease the number of memory allocations , copies, and deallocations that occur as a result of creating a new internal array.

A major disadvantage of using an ArrayList is that it acts as a list rather than an array. An ArrayList is in essence a single-dimension array. Now that's not saying you couldn't emulate a multidimensional array using an ArrayList . Items within an ArrayList must derive from System.Object as all classes do. You can place any class within the .NET Framework within an ArrayList , and because System.Array derives from System.Object , you can add an entire array reference to a single item within an Array . This technique is the same technique you use to create a jagged array in which items at a certain index contain additional arrays, creating arrays within arrays.

One area of concern is performance. As mentioned earlier, an ArrayList has to allocate memory for an additional array, copy the memory of the original array to the new array, and then destroy the original array. If this process happens over and over again, you see an impact on performance. Additionally, when an item is added to an ArrayList , that item is converted to a System.Object . For .NET classes, this conversion isn't a problem because all classes ultimately derive from System.Object . For value types, however, the value must first be boxed. Boxing refers to the process of converting a value type into a class that is derived from System.Object . For instance, if you add an integer (data type int ) to an ArrayList , it is first packaged as a System.Object , which means memory is allocated from the heap and type information is created for that integer. Furthermore, when you need to retrieve the item from the ArrayList , you must unbox it, which, as its name implies, is the reverse process of boxing; an object is converted back to its original value type using the type information associated with that object. If the items within the ArrayList are continually boxed and unboxed, this process too will have an impact on performance.

 <  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