Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between array and ArrayList?

What is an array and what is an ArrayList? What is the difference between them?

like image 909
Surya sasidhar Avatar asked Oct 12 '09 04:10

Surya sasidhar


1 Answers

An Array is a high performance way of storing a group of data with the same type because each element is laid out next to it's neighbor in memory. This allows for very fast access because (a) the code can do a little math and jump quickly to any location in the array, and (b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses). An array in .NET is actually a class (System.Array), but a special type of class that is well understood by the .NET engine (CLR). Because of this, you can standard array access notation (text languages) such as foo[3] = 99;

In ArrayList, however, you are dealing with a collection. There are several types of collections in .NET (see the System.Collections and System.Collections.Specialized namespaces), but the key thing about them is the interfaces they support (IEnumerable, ICollections, IList, etc). If you look at the definition of these interfaces, you see that collections are all about grouping things together and providing methods to access them.With an ArrayList, however, if you add one element more than the internal array can handle, the ArrayList automatically creates a larger array and copies the old array into the new array.

like image 63
Arsen Mkrtchyan Avatar answered Sep 20 '22 20:09

Arsen Mkrtchyan