HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

ArrayList Collection Type
ArrayList is a collection class present in the System.Collections namespace. Below example shows how to add 5 integere elements to an arraylist.
  • In our example We create an instance of an arraylist class.
               ArrayList AL = new ArrayList();
    We can also create an ArrayList instance as shown below. The number 5 that we passed to the ArrayList constructor is the capacity of the ArrayList. If we donot specify the capacity, .NET will assign a default capacity of 4 elements. When we add a 5th element the arraylist size will be automatically increased by 100%. This means the capacity will be automatically increased to 8 elements. To automatically increase the capacity, a new ArrayList with capacity of 8 elements is created and the contents from the previous arraylist are copied into this newly created arraylist. This happens automatically.
              ArrayList AL = new ArrayList(5);
  • We then use the Add() method to add elements to the ArrayList object.
  • We then used Capacity property to determine how many elements an arraylist can hold.
  • Count property is used to determine how many elements are currently present in the arraylist.
  • Insert() method can be used to insert a new element any where in the arraylist. In our example we are inserting number 5 at index 1 using the insert() method.
  • We can also sort an arraylist using the sort() method.

using System;

using System.Collections;

 

class ArrayListDemo

{

    public static void Main()

    {

        //Create an instance of ArrayList Class

        ArrayList AL = new ArrayList();

        //Add elements to an array using the add() method

        AL.Add(10);

        AL.Add(20);

        AL.Add(30);

        AL.Add(40);

        AL.Add(50);

        //Use Capacity property to determine how many elements an arraylist can hold

        Console.WriteLine("Capacity = " + AL.Capacity);

        //Use Count property to determine how many elements are currently present

        Console.WriteLine("Elements in Array = " + AL.Count);

        foreach (int i in AL)

        {

            Console.WriteLine(i);

        }

        //Use the insert method to insert an element at a specific index.

        AL.Insert(1, 5);

        Console.WriteLine("After inserting 5 in the arraylist between 10 and 20");

        foreach (int i in AL)

        {

            Console.WriteLine(i);

        }

        //Sort an array list using the Sort() Method

        AL.Sort();

        Console.WriteLine("After the Array is Sorted");

        foreach (int i in AL)

        {

            Console.WriteLine(i);

        }

    }

}
Adding more than 1 element at time to an arraylist
So far we have seen how to add only one element at time to an arraylist using the add() method. We have an AddRange() method which can be used to add more than one element at a time. Below example demonstrates this.
  • We have ArrayList AL1 to which we add 10,20,30 and 40 using Add() method.
  • We then create another ArrayList AL2 to which we add 1000,2000 and 3000 using the Add() method.
  • AddRange() method is then used to add all the elements present in AL2 into AL1.

using System;

using System.Collections;

class ArrayListDemo

{

    public static void Main()

    {

        ArrayList AL1 = new ArrayList();

        //Add elements to an array using the add() method

        AL1.Add(10);

        AL1.Add(20);

        AL1.Add(30);

        AL1.Add(40);

        ArrayList AL2 = new ArrayList();

        AL2.Add(1000);

        AL2.Add(2000);

        AL2.Add(3000);

        //Use the AddRange() Method to add more than one element at a time.

        AL1.AddRange(AL2);

        foreach (int i in AL1)

        {

            Console.WriteLine(i);

        }

    }

}

Adding multiple data types to an array list
If you have an array of type integer we can only add integers to the array. If the array type is string then we can only add strings to the array. To an arraylist we can add any type of object as shown in the example below.
  • To the arraylist we are adding a string,integer and a DateTime object. This is possible bcos arraylist stores these data types as object data type. All types in .NET derive from object type. Hence we can store any data type in an arraylist. This is one of the main difference between an array and an arraylist.
  • We than use the foreach loop and print out the contents of the array.

using System;

using System.Collections;

class ArrayListDemo

{

    public static void Main()

    {

        ArrayList AL1 = new ArrayList();

        AL1.Add("Prasad");

        AL1.Add(30);

        AL1.Add(DateTime.Now);

        foreach (object obj in AL1)

        {

            Console.WriteLine(obj.ToString());

        }

    }

}

Performance
In general arrays are faster than arraylist. Let us understand with an example why arrays are faster than arraylist. In the example below
  • We are adding 2 integers 10 and 20 to the arraylist AL1. 10 and 20 are integers but arraylist stores these integers as objects internally. So the runtime needs to convert these integer value types to object types. Converting a value type to a reference type is called boxing and is expensive to perform.
  • We then use the foreach loop to retrieve each integer present in the AL1 arraylist into a variable i of type integer. We know 10 and 20 are stored as object types, which need to be converted to integer. So we need to perform unboxing(Converting reference types to value types).
  • Boxing and unboxing are expensive operations to perform. Arrays does not have this overhead. Hence arraylists are a little slower than arrays.

using System;

using System.Collections;

class ArrayListDemo

{

    public static void Main()

    {

        int Sum = 0;

        ArrayList AL1 = new ArrayList();

        AL1.Add(10);

        AL1.Add(30);

        foreach (int i in AL1)

        {

            Sum += i;

        }

        Console.WriteLine("Sum = " + Sum);

    }

}

Sorting an arraylist in descending order
To sort an arraylist in descending order 
  • First apply the sort() method on the arraylist.
  • Then apply the reverse() method.

using System;

using System.Collections;

class ArrayListDemo

{

    public static void Main()

    {

        ArrayList AL1 = new ArrayList();

        AL1.Add(1);

        AL1.Add(4);

        AL1.Add(2);

        AL1.Add(5);

        AL1.Add(3);

        //Apply Sort() and then the reverse() method

        AL1.Sort();

        AL1.Reverse();

        foreach (int i in AL1)

        {

            Console.WriteLine(i);   

        }

    }

}

Other useful methods
  • Clear() - Removes all elements from the arraylist
  • Clone() - Creates a shallow copy of the arraylist
  • Contains() - Checks if an element is present in an arraylist.
  • CopyTo() - Copies the arraylist elements into an array
  • TrimToSize() - Sets the capacity to actual number of elements in the arraylist.
Difference between arrays and arraylists
  • Array is in the System namespace where as ArrayList is in the System.Collections namespace.
  • The capacity of an Array is fixed, whereas the capacity of an ArrayList is automatically expanded by 100% when the ArrayList is completely filled.
  • ArrayList provides methods that add, insert, or remove a range of elements. In Array, you can get or set the value of only one element at a time.
  • An Array can have multiple dimensions, while an ArrayList always has exactly one dimension
  • An Array of a specific type (other than Object) has better performance than an ArrayList because the elements of ArrayList are of type Object and, therefore, boxing and unboxing typically occur if storing or retrieving a value type.
Other related articles

Njoy Programming
   ByPrasad Cherukuri