Tuesday, September 9, 2008

Arrays and ArrayLists

What is an Array?
Array is a group of similar data types so we can simply say Array's are homogeneous data structures. Array can store the elements in contiguous memory locations.We can access elements in array by index.
using array's
. we can easily store the data
. we can easily sort the data
. we can re size the array
Types of Array's:
. One dimensional array
int[] abc ;
int[] abc = new int[10];
. Multi dimensional array
int[,] abc;
int[,] abc = new int[2,3];
. Jagged array
arrays of arrays is called jagged arrays.
string[][] abc = new string[2][];

Disadvantage of an array's:
consider one array with 100 elements. suppose we want search an element of 99th it visit every element while searching so performance wise it is not good. To overcome arrays MS developed collections.
-------------------------------------------------------------------------------------------------
ArrayLists
ArrayList same as standard array. It supports dynamic array,which changes it's size as required. array can store only one type elements but ArrayList can store different types of object so ArrayLists are heterogeneous data structure. In ArrayList it re size allocation memory automatically when storing reach the array limits it double the size of an array. We can fix the size by assigning value to capacity parameter.

ArrayList can store collection of objects. It means it can store everything as a object. This is known as Boxing. In case of retrieving a value from this you need to cast the element of the object array to original type.This is known as unboxing.

Disadvantage:
sorting is not possible.

No comments: