Collection - ArrayList
Disadvantage of chronic Array or Array class:
1. Memory Wastage
2. Memory Shortage * If you do not know in advance how many objects an array will hold, you run the risk of declaring either too small an array (and running out of room) or too large an array (and wasting memory).
3. Array is collection of elements which is similar type * Your program might be asking the user for input, or gathering input from a web site. As it finds objects (strings, books, values, etc.) you will add them to the array
4. The size of array is always constant.
5. Size of array must be defined or specified at the time of declaration or instantiation.
6. Array is not flexible for retrieving and adding elements in the array.
Definition: * The ArrayList class is an array whose size is dynamically increased as required.
Data Type: * Reference type.
Storage Location * On Heap
Namespace
* ArrayList class is defined in System.Collection namespace. * While using ArrayList class you must import System.Collection namespace.
i.e. using System.Collection
Difference between Array & ArrayList class * Namespace:-
Array:-System.
ArrayList:-System.Collection. * Size of length
Array:-Length
ArratList:-Count * Methods
Array:-static & property
ArrayList:-public, static & property.
Collection
* Collection is grouping of classes that store elements of type Object and generic collection classes.
When to use ArrayList class * If the number of elements is dynamic, you should use a collection class.
Syntax * ArrayList brm =new ArrayList( );
ArrayList: - class (keyword) brm: - object (identifier) new: - for memory allocation (keyword)
Adding elements in the ArrayList (Add () method) * By using Add () method we can add elements in the ArrayList. * ArrayList method Add appends a new element at the end of an ArrayList. * Add () is a public method accesed by