Saturday, October 18, 2008

Difference beteween Array and ArrayList

Array
An array is a data structure that contains a number of variables of the same type. Array Provides methods for creating, manipulating, searching, and sorting arrays. An array can be Single-Dimensional, Multidimensional or Jagged. Arrays are zero indexed: that is, an array with n elements is indexed from 0 to n-1. The default value of numeric array elements are set to zero, and reference elements are set to null. A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

ArrayList
ArrayList Represents a dynamically sized, index-based collection of objects. It Implements the IList interface using an array whose size is dynamically increased as required. The capacity of an ArrayList is the number of elements the list can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation.

Difference
1. The capacity of an Array is fixed, whereas the capacity of an ArrayList is automatically increased as required. Arrays cannot be changed in size at runtime (except using 'ReDim' which will create a new array, copy the old array in the new array and destroy the old array).
2. ArrayList provide methods that add, insert, or remove a range of elements. In Array, we can get or set the value of only one element at a time.
3. ArrayList provide methods that return read-only and fixed-size wrappers to the collection. Array does not.
4. We can set the lower bound of an Array, but the lower bound of an arrayList is always zero.
5. An Array can have multiple dimensions, while an ArrayList can have only one dimension.
6. An Array of a specific type (other than Object) has better performance than an ArrayList because the elements of ArrayList are of type Object and, therefore, boxing and unboxing typically occur when storing or retrieving a value type.
7.Array is in the System namespace. ArrayList is in the System.Collections namespace.

No comments: