Jun 1, 2010

Array vs List vs ArrayList: When to use which?


Arrays
An array allows a collection of values of the same type to be stored and accessed via a single variable. Each item is accessed in the array variable through the use of an index value into the array.
A C# array may be created in a number of different ways.
  • SingleDimensional array
  • MultiDimensional array
  • Jagged Arrays
  1. A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
  2. When you Preper array - You know the size of an array at the time that it is created.
  3. You should not use arrays when you don't know the size of the array, or the size is likely to change. However, if you have a fixed length array, they are an easy way of retrieving elements by index.

Since resizing arrays is expensive.


LIST <T>
  • Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class.
  • It implements the IList generic interface using an array whose size is dynamically increased as required.
  • If you're going to be adding/removing elements a lot, just use a List. If it's multidimensional, you can always use a List<list<int>>

ArrayList
  • Array List is untyped list, it doesn't know what is in there (and indeed,you could add any combination of objects).
  • The ArrayList is not guaranteed to be sorted. You must sort the ArrayList prior to performing operations (such as BinarySearch) that require the ArrayList to be sorted.
  • The capacity of a ArrayList is the number of elements the ArrayList can hold. As elements are added to a ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
  • The ArrayList collection accepts null as a valid value, allows duplicate elements.
  • Using multidimensional arrays as elements in an ArrayList collection is not supported.

The conclusions
  • Always use arrays or List's when possible, when working with valuetypes. The big overhead is probably due to the boxing that takes place (an Int32 object has to be created for every number, and a reference to it is stored in the ArrayList).
  • When working with reference-type data (like strings) it's typically not worth the effort to change it to Lists unless you are dealing with really big amounts of data. The List is only around 110 ms faster than the arraylist when dealing with 8 mil. items and both seem to scale linearly.
  • When possible it really does help a lot to initialize your list or ArrayList to the expected size. It makes the performance approach that of a normal array.When Microsoft claims that they've improved performance a great deal for collection-types in framework 2.0, it's only true for the "new" types like List. There's almost no difference in ArrayList performance!
  • ArrayLists are essentially deprecated as they're untyped - you need to use casts with them - and they're slower and less space efficient for value types because they require the items to be boxed.
  • Generic lists were introduced with .Net 2.0 and are the way to go. Often a List is better than an array, with few downsides.
  • You cannot compare the performance of Array and List, since the arrays I simply threw in as a baseline, since most lists internally uses array (in various combinations) to store their data.

When to use which?

  1. Array is Best : when its fixed length.
  2. List is Best : When typed list of objects.
  3. ArrayList is Best : When adding multiple combination of objects.

No comments:

Post a Comment