Home > DeveloperSection > Articles > ArrayList Class in C#

ArrayList Class in C#


C# C#  Collection 
Ratings:
0 Comment(s)
 1138  View(s)
Rate this:

 

ArrayList Class in C#  



Introduction:


The ArrayList class can store heterogeneous collections of objects unlike array which store homogeneous collection of data. Basically, ArrayList class is an alternative of array. Using ArrayList you can add and remove items from a list at specified index. ArrayList also allows dynamic memory allocation for adding, searching and sorting items in the list.


Multidimensional arrays is not supported by the ArrayList.  As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. You can decreases your ArrayList size by calling TrimToSize or by setting the Capacity property explicitly.


The ArrayList collection accepts null as valid value. It also allows duplicate item.


ArrayList Constructors:


1.  ArrayList (): Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.

2.   ArrayList (ICollection): Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.

3.  ArrayList (Int32): Initializes a new instance of the empty ArrayList class with initial capacity passed as parameter.

 

 For creation of ArrayList using its default constructor, code is as follows:


ArrayList arrayList = new ArrayList();
// adding elements in the ArrayList
arrayList.Add(18); // you can define any type of objects such as Integer, String etc.
// retrieving elements
foreach ( var p in arrayList)
{
    
Console.WriteLine(p+ );
}


Properties and Methods of ArrayList:


   Some important Properties and Methods of ArrayList are:


1.  Capacity: For checking the capacity of ArrayList object. By default, ArrayList capacity is 4. If you add 5th element in the list then its capacity will be incremented by its default capacity, this case its capacity will become 8. Its capacity will remain unchanged till we have less than 9 elements but as soon as we add its capacity will automatically be increased by default capacity i.e. 4 and its new capacity will be 12.

 We can get current capacity of arraylist using “Capacity” property

Example: arrayList.Capacity

 2.       Sort: For sorting a ArrayList, we use sort() method:

    Example: arrayList.Sort();

 3.       Reverse: For reversing a ArrayList, we use reverse() method.

Example: arrayList.Reverse();

 4.       Remove: For removing all elements in ArrayList, we use remove() method.

Example: arrayList.Remove();

 We can also remove single element at a time by using RemoveAt(int index) method.

Example: arrayList.RemoveAt(3);

 5.       IndexOf(object): For finding index number of specified object, we use IndexOf(object) method.

Example: arrayList.IndexOf(25);

 

Similarly, there are other helpful properties that are defined in ArrayList class which we can use. For complete list of properties and methods visit following link:

https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx

 

Below is an example to demonstrate the use of ArrayList.

using System;
using System.Collections;
 
namespace DemoArrayList
{
class Program  
{      
static void Main(string[] args)    
{
            Console.WriteLine(@"## Lets us try to use ArrayList class in a program");
            Console.WriteLine();
            ArrayList arrayList = new ArrayList();
            Console.WriteLine("Initial Capacity: " + arrayList.Capacity);
            Console.WriteLine();
            Console.WriteLine("Enter the five element in ArrayList: ");
            Console.WriteLine();
            for (int i = 0; i < 5; i++)
            {
                arrayList.Add(Console.ReadLine());
            }
            Console.WriteLine();
            Console.WriteLine(@"## Retrieving data from the list:");
            //Console.WriteLine();
            //Console.WriteLine();
            foreach (var p in al)
            {
                Console.Write(p.ToString() + " ");
            }
            Console.WriteLine();
            Console.WriteLine("After adding 5 elements capacity is: "+ arrayList.Capacity);
            Console.WriteLine();
            arrayList.Reverse();
            Console.WriteLine();
            Console.WriteLine(@"## Reversing item to the list: ");
            Console.WriteLine();
            foreach (var p in arrayList)
            {
                Console.Write(p.ToString() + " ");
            }
            Console.ReadKey();
}   
}
}

Output:

 

Advantage of ArrayList:

 

·         The ArrayList stores everything as object so we can store almost any value regardless of their data types in a single list unlike array.

·         No need to do explicitly allocation or deallocation for storing a data.

·         You can insert and delete the elements between positions in the ArrayList.



Don't want to miss updates? Please click the below button!

Follow MindStick