Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 24 June 2016

What are the differences in Array and ArrayList and when to use array or ArrayList and which is better in performance

Arrays
Arrays are strongly typed collection of same datatype and  fixed length that cannot be changed during runtime. Generally in arrays we will store values with index basis that will start with zero. If we want to access values from arrays we need to pass index values.

Declaration of Arrays
string[] arrNames=new string[3];

Add value in Array at particular index
string[] arrNames=new string[2];
arrNames[0] = "Vijay";
arrNames[1] = "Ajay";
arrNames[1] = "Deepak";

Arraylists
Array lists are not strongly type collection. It will store values of different datatypes or same datatype. Array list size will increase or decrease dynamically it can take any size of values from any data type.

Declaration of Arraylist
ArrayList arrMiscellaneous = new ArrayList();
arrMiscellaneous.Add("Vijay"); // Add string values
arrMiscellaneous.Add(15);   // Add integer values
arrMiscellaneous.Add(25.25); // Add float values

Difference between Array and ArrayList


Array
ArrayList
They are fixed length.
They are resizable and variable length.
They are compiled strong type collection.
They are flexible and can accommodate any data types.
Because arrays are of fixed size and strong type collection
performance is faster.
In arraylist lots of boxing and unboxing are done there for its
performance is slower.
Arrays belong to System.Array namespace.
 Arraylist belongs to System.Collection namespaces.

When to use Array or ArrayList
If you have fixed length data and also have strongly typed collection then you should use Array.
If you  have automatically growing size and generics collection then you should use ArrayList .

Performance
Array is faster than ArrayList As in ArrayList lots of Boxing and UnBoxing are done therefore its performance is slower than Array because in Array there is no type casting.

No comments:

Post a Comment