Ghghghj

Only available on StudyMode
  • Download(s) : 409
  • Published : March 24, 2011
Open Document
Text Preview
LECTURE NO.10

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 using instance of ArrayList class. * The Add () method takes as object its parameter.

* Example

Public class
{
Static void Main ()
{
ArrayList brm =new ArrayList ();
Brm. Add (1);
Brm. Add (2);
Brm. Add (3);
}
}
Accessing elements in the ArrayList
* We can display in the array by three ways
1. By using for each loop
Public class
{
Static void Main ()
{
ArrayList brm =new ArrayList ();
Brm. Add (1);
Brm. Add (2);
Brm. Add (3);

foreach (int ex in brm)
{
Console.WriteLine (ex);
}
}
}

OUTPUT: 1 2 3.

2. By using Index operator ([]).
Public class
{
Static void Main ()
{
ArrayList brm =new ArrayList ();
Brm. Add (1);
Brm. Add (2);
Brm. Add (3);

for (int i=0; I < 3; i++)
{
Console.WriteLine (“{0}”, Brm [i]);
// Console.WriteLine (Brm[i]);
}
}
}

3. By using unboxing process
* ArrayList class from the namespace System. Collections stores objects. * Add () method is defined to require an object as a parameter, and so an integer type is boxed. * When the values from an ArrayList are read, unboxing occurs when the object is converted to an integer type. * Example

ArrayList list = new ArrayList ();

list. Add (44); // boxing - convert a value type to a reference type

int i1 = (int) list [0]; // unboxing - convert a reference type to

// a value type

foreach (int i2 in list)
{
Console.WriteLine (i2); // unboxing
}

Declaring initial Capacity of ArrayList (Capacity property)

* Array class, you define how many objects the array will hold. If you try to add more than that, the Array class will throw an exception. * ArrayList you do not declare how many objects the ArrayList...
tracking img