Difference between an Array and ArrayList

Posted by Techie Cocktail | 2:10 PM | | 1 comments »

Below are some differences.

Array:
a. The Array size needs to be defined or must have an Array Initializer at the time
of declaration. Their size remains fixed once defined and created.


string[] strArray = new string[10];
string[] strArray = { "one", "two", "three" };

b. The Array class can be found in System namespace.
c. An Array can hold similar types.
d. Arrays can be multi-dimensional.
e. Arrays are strongly typed to a type of which they are declared. For e.g string[], char[].
int[]
f. Array do not have boxing/unboxing overhead.

ArrayList:
a. Arraylist does not requires to define its size at the time of declaration. Its size
increases or decreases dynamically as you add/remove elements from the arraylist.

ArrayList arrLst = new ArrayList();
arrLst.Add("one");
arrLst.Remove("one");

b. The Arraylist class can be found in System.Collections namespace.
c. Arraylist does not have any restrictions on the types of objects that can be added.
d. An Arraylist is a single dimension collection.
e. Arraylist is not strongly typed to a single type, it can contain any types.
f. Arraylist have the overhead of boxing/unboxing while adding/retriving values from it.

1 comments

  1. Anonymous // May 19, 2009 at 6:59 AM  

    The concept is explained very clearly.Thank u :-)