C# ArrayList

Introduction

ArrayList implements the IList interface using an array whose size is dynamically increased as required. In this article I explain how to create the ArrayList and the various methods and properties of the ArrayList.

Creation of ArrayList

To create the ArrayList the following code is used:

ArrayList arr = new ArrayList();

 

The datatype of an ArrayList is object type, so we can add the elements having the datatype string, integer and any other.


Add the elements in ArrayList

To add values, the Add() method is used. 

The following code describes how we add the elements to the ArratList.

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements of the ArrayList are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }           

        }

    }

}

 

Output

arraylist_add.jpg

Properties

Capacity

Gets or sets the number of elements that the ArrayList can contain. The default capacity of an ArrayList is 4. If I add 5 elements then its capacity becomes doubled (that is 8) relative to the previous one (that is, 4). 

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("Sunday");

            arr.Add("Monday");

            Console.WriteLine("**********Elements of ArrayList*************");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("Capacity of the ArrayList is:" + arr.Capacity);

            Console.WriteLine();

            Console.WriteLine("//Adding Three more elements");

            Console.WriteLine("**********Elements of ArrayList*************");

            Console.WriteLine();

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("Now the Capacity is:" + arr.Capacity);

            Console.WriteLine();

            Console.WriteLine("//Adding Four more elements");

            Console.WriteLine("**********Elements of ArrayList*************");

            arr.Add("Friday");

            arr.Add("Saturday");

            arr.Add("Januray");

            arr.Add("Feburay");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("Now the Capacity is:" + arr.Capacity);

        }

    }

}

 

Output

arraylist_capacity.jpg

Count

Gets the number of elements actually contained in the ArrayList

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("Sunday");

            arr.Add("Monday");

            Console.WriteLine("**********Elements of ArrayList*************");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("The number of element in the ArrayList are:" + arr.Count);

            Console.WriteLine();

            Console.WriteLine("//Adding Three more elements");

            Console.WriteLine("**********Elements of ArrayList*************");

            Console.WriteLine();

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("Now the number of elements in the ArrayList are:" + arr.Count);

            Console.WriteLine();

            Console.WriteLine("//Adding Four more elements");

            Console.WriteLine("**********Elements of ArrayList*************");

            arr.Add("Friday");

            arr.Add("Saturday");

            arr.Add("Januray");

            arr.Add("Feburay");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("Now the elements in the ArrayList are:" + arr.Count);

        }

    }

}

 

Output

arraylist_count.jpg

Methods

Clear() 

Removes all elements from the ArrayList

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine("The number of element in the arraylist are:" + arr.Count);

            arr.Clear();

            Console.WriteLine("After apply the clear method the elements in the arraylist are:" + arr.Count);

        }

    }

}

 

Output

arraylist_clear.jpg

Contains() 

Determines whether an element is in the ArrayList

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine();

            Console.WriteLine("The element Saturday contain in the arraylist is:" + arr.Contains("Saturday"));

            Console.WriteLine("The element Monday contain in the arraylist is:" + arr.Contains("Monday"));

            Console.WriteLine("The element Tuesday contain in the arraylist is:" + arr.Contains("Tuesday"));

            Console.WriteLine("The element May contain in the arraylist is:" + arr.Contains("May"));

            Console.WriteLine("The element Hello contain in the arraylist is:" + arr.Contains("Hello"));

        }

    }

}

 

Output

arraylist_contains.jpg

Insert()

Inserts an element into the ArrayList at the specified index.

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Insert(0, "********Names of the days*******");

            arr.Insert(8, "*********Names of the months**********");

            arr.Insert(9, "Janurary");

            arr.Insert(10, "Feburary");

            arr.Insert(11, "March");

            arr.Insert(12, "April");

            arr.Insert(13, "May");

            Console.WriteLine("The elements in the arraylist are after the insert operation:");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

arraylist_insert.jpg

InsertRange()

Inserts the elements of a collection into the ArrayList at the specified index. 

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Stack stack=new Stack();

            stack.Push("Janurary");

            stack.Push("Feburary");

            stack.Push("March");

            stack.Push("April");

            stack.Push("May");

            stack.Push("June");

            stack.Push("July");

            stack.Push("August");

            stack.Push("September");

            stack.Push("October");

            stack.Push("November");

            stack.Push("December");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Insert(0, "********Names of the days*******");

            arr.Insert(8, "*********Names of the months**********");

            arr.InsertRange(9, stack);

            Console.WriteLine("The elements in the arraylist are after the insert operation:");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

arraylist_insertrange.jpg

IndexOf(object)

Searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine("The index value of Sunday is:" + arr.IndexOf("Sunday"));

            Console.WriteLine("The index value of Monday is:" + arr.IndexOf("Monday"));

            Console.WriteLine("The index value of Tuesday is:" + arr.IndexOf("Tuesday"));

            Console.WriteLine("The index value of Wednesday is:" + arr.IndexOf("Wednesday"));

            Console.WriteLine("The index value of Thusday is:" + arr.IndexOf("Thusday"));

            Console.WriteLine("The index value of Friday is:" + arr.IndexOf("Friday"));

            Console.WriteLine("The index value of Saturday is:" + arr.IndexOf("Saturday"));

        }

    }

}

 

Output

arraylist_indexof.jpg

Remove()

Removes the first occurrence of a specific object from the ArrayList

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Remove("Sunday");

            arr.Remove("Monday");

            arr.Remove("Tuesday");

            Console.WriteLine();

            Console.WriteLine("After remove the elements the arraylist is as:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

arraylist_remove.jpg

RemoveAt()

Removes the element at the specified index of the ArrayList

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.RemoveAt(3);

            arr.RemoveAt(1);

            arr.RemoveAt(2);

            Console.WriteLine();

            Console.WriteLine("After remove the elements the arraylist is as:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

arraylist_removeat.jpg

RemoveRange()

Removes a range of elements from the ArrayList.

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList(7);

            arr.Add("Sunday");

            arr.Add("Monday");

            arr.Add("Tuesday");

            arr.Add("Wednesday");

            arr.Add("Thusday");

            arr.Add("Friday");

            arr.Add("Saturday");

            Stack stack = new Stack();

            stack.Push("Janurary");

            stack.Push("Feburary");

            stack.Push("March");

            stack.Push("April");

            stack.Push("May");

            stack.Push("June");

            stack.Push("July");

            stack.Push("August");

            stack.Push("September");

            stack.Push("October");

            stack.Push("November");

            stack.Push("December");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Insert(0, "********Names of the days*******");

            arr.Insert(8, "*********Names of the months**********");

            arr.InsertRange(9, stack);

            Console.WriteLine("The elements in the arraylist are after the insert operation:");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.RemoveRange(9, 12);

            Console.WriteLine();

            Console.WriteLine("After apply the RemoveRange operation the elements are:");

            Console.WriteLine();

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

arraylist_removerange.jpg

Reverse()

Reverses the order of the elements in the entire ArrayList.

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add(1);

            arr.Add(23);

            arr.Add(12);

            arr.Add(11);

            arr.Add(45);

            arr.Add(20);

            arr.Add(54);

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Reverse();

            Console.WriteLine("After apply reverse methods the elements are as:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }           

        }

    }

}

 

Output

arraylist_reverse.jpg

Sort()

Sorts the elements in the entire ArrayList.

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add(1);

            arr.Add(23);

            arr.Add(12);

            arr.Add(11);

            arr.Add(45);

            arr.Add(20);

            arr.Add(54);

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Sort();

            Console.WriteLine("After apply sort methods the elements are as:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }           

        }

    }

}

 

Output

arraylist_sort.jpg

BinarySearch(object) 

Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.  

For example:

namespace ArrayList1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add(1);

            arr.Add(23);

            arr.Add(12);

            arr.Add(11);

            arr.Add(45);

            arr.Add(20);

            arr.Add(54);

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            arr.Sort();

            Console.WriteLine("The sorted list is as:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine("The element 23 searched at:"+arr.BinarySearch(23));

            Console.WriteLine("The element 12 searched at:" + arr.BinarySearch(12));                      

        }

    }

}

 

Output

arraylist_binarysearch.jpg  

Summary

In this article I explained the ArrayList and the various methods and properties of ArrayList.

Up Next
    Ebook Download
    View all
    Learn
    View all