Friday, June 4, 2010

ARRAYLIST CLASS


ARRAYLIST CLASS
An array list is very similar to an array, except that it has the ability to grow. It is represented by the class System.Collections.ArrayList.
The simplest form of instantiation of an ArrayList is illustrated here:
ArrayList baseballTeams = new ArrayList();
  In order to get at the ArrayList class, user need to make a reference to the System.Collections namespace in your project. User can  instantiate an ArrayList by indicating the initial capacity user want. For example:
ArrayList baseballTeams = new ArrayList(20);
It is also possible to set the capacity of the ArrayList directly after a generic instantiation with the use of the Capacity property as shown in the following example:
ArrayList baseballTeams = new ArrayList();
baseballTeams.Capacity = 20
Once the ArrayList is instantiated, user can then add elements using the Add() method. This console application example uses the Add() method and then displays each of the items that were added:
ArrayList baseballTeams = new ArrayList();
baseballTeams.Add("St. Louis Cardinals");
One thing to note about adding items to the ArrayList is that the ArrayList treats all its elements as object references. That means you can store whatever objects user like in an ArrayList, but when accessing the objects, user will need to cast them back to the appropriate data type, as shown here:
string element1 = (string)baseballTeams[1];
When adding items to the ArrayList, user can also specify the location of the item being added using the Insert() method. This is illustrated in the following example:
baseballTeams.Insert(1, "San Francisco Giants"); // inserts at position 1
User can remove elements at a specific point in the collection with the use of the RemoveAt() method. This is done using the following construction:
baseballTeams.RemoveAt(1); // removes object at position 1

SOME IMPORTANT ARRAYLIST METHODS AND PROPERTIES
Methods/property
Description
Add()
Clear()
Contains()
CopyTo()
Insert()
Remove()
RemoveAt()
RemoveRange()
Sort()
Capacity
Count
Adds an object to a list
Removes all the elements from the list
Determines if an element is in the list
Copies a list to another
Inserts an element into the list
Removes the first occurrence of an element
Removes the element at the specified place
Remove a range of elements
Sorts the element
Gets or sets the number of elements in the list
Gets the number of elements currently in the list

using System;
using System.Text;
using System.Collections;

   class Program
    {
        static void Main(string[] args)
        {   //Creating an ArrayList
            ArrayList names = new ArrayList();
            //Adding the elements to the ArrayList
            Console.WriteLine("Using Add().......");
            names.Add("Aditya Tyagi");//Add(Object obj)
            names.Add("Dev Pratap");
            names.Add("Arun Kumar");
            names.Add("Arun Solanki");
            names.Add("Deepak");
            names.Add("Rajesh");
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }
            //Using Remove()
            Console.WriteLine("Using Remove()........");
            names.Remove("Rajesh");Remove(object obj)
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }
            //Using RemoveAt()
            Console.WriteLine("Using RemoveAt().......");
            names.RemoveAt(4);//RemoveAt(int index)
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }
            //Using Insert()
            Console.WriteLine("Using Insert().......");
            names.Insert(4, "Deepak");//Insert(int index,object obj)
            names.Insert(5, "Rajesh");
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }
            //Using RemoveRange()
            Console.WriteLine("Using RemoveRange().......");
            names.RemoveRange(4, 2);//RemoveRange(int index,int count)
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }

            //Using Sort()
            Console.WriteLine("Using Sort().......");
            names.Sort();
            for (int i = 0; i < names.Count; i++)
            {
                Console.WriteLine(names[i]);
            }

            //Using Contains()
            Console.WriteLine("Using Contains().......");
            bool b= names.Contains("Aditya Tyagi");//Contains(object obj)
            Console.WriteLine(b);

            //Using Clear()
            Console.WriteLine("Using Clear().......");
            names.Clear();
            Console.WriteLine(names.Count);
           
            




        }
    }

Output:
Using Add().......
Aditya Tyagi
Dev Pratap
Arun Kumar
Arun Solanki
Deepak
Rajesh
Using Remove()........
Aditya Tyagi
Dev Pratap
Arun Kumar
Arun Solanki
Deepak
Using RemoveAt().......
Aditya Tyagi
Dev Pratap
Arun Kumar
Arun Solanki
Using Insert().......
Aditya Tyagi
Dev Pratap
Arun Kumar
Arun Solanki
Deepak
Rajesh
Using RemoveRange().......
Aditya Tyagi
Dev Pratap
Arun Kumar
Arun Solanki
Using Sort().......
Aditya Tyagi
Arun Kumar
Arun Solanki
Dev Pratap
Using Contains().......
True
Using Clear().......
0
Press any key to continue . . .