static void Main(string[] args)

ArrayList

Posted on

ArrayList:

  • The ArrayList is one of the important classes in the System.Collection namespace.  ArrayList is a dynamic array; it will increase the size of the storage location as required. It stores the value as object. The allocation of the ArrayList can be achieved through the TrimToSize property.
  • In array you can store data only of same data type. For ex-if array has int then char cannot be stored. But this is possible with collection known as hash table.
  • Collections allow easy sorting, deleting and adding of items.
  • Array list work similar to an array.
  • ArrayList has many predefined methods listed below-
Add It will add the element as object in the ArrayList
Clear It will clear the all objects in the ArrayList
Insert It will insert the element in the specified location of the index in the ArrayList.
InsertRange It will insert the elements in the object as individual objects in the specified location.
Remove It will remove the given object in the first occurrence in the ArrayList.
RemoveAt It will remove the object as specified in the argument.
RemoveRange It will remove the set of objects in the ArrayList from the range specified.
Sort It will do the sorting of the elements in the ascending order.
Reverse It will arrange the elements in the reverse order in the ArrayList.
Contains It checks whether the objects exists or not.

 static void Main(string[] args)

{   ArrayList al = new ArrayList();

//adding item to arraylist

al.Add(“ramesh”);

al.Add(“suresh”);

al.Add(“sita”);

al.Add(“gita”);

Console.WriteLine(“add the names to the list”);

foreach (string a in al)

{

Console.WriteLine(“{0}”,a );

}

//sorting item to arraylist

al.Sort();

Console.WriteLine(“sorting names in the list”);

foreach (string a in al)

{

Console.WriteLine(“{0}”, a);

}

//removing item from arraylist

Console.WriteLine(“removing names from the list”);

al.Remove(“ramesh”);

//showing item from arraylist

foreach (string a in al)

{

Console.WriteLine(“{0}”, a);

}

Console.Read();       }    }

 

OUTPUT –

add the names to the list

ramesh

suresh

sita

gita

sorting names in the list

gita

ramesh

sita

suresh

removing names from the list

gita

sita

suresh