How To Use The Array List Class In C#



After reading this article, you will learn about

  • ArrayList.
  • Uses of an ArrayList.
  • How to access the elements in an ArrayList.
  • Types of used in Methods in an ArrayList.

The first and foremost thing to remember about an array is that it does not have a fixed size; it grows, as required, always.

  • Use Add (object) to add an object to the end of the ArrayList.
  • Use [] to access the elements in the ArrayList.
  • Use TrimToSize() to reduce the size to fit the number of elements in the ArrayList.
  • User clears to remove all the elements.

Actually, ArrayList can solve the main disadvantage of an array, which is that we must know the capacity of the data structure, when you instantiate the array by providing a data structure that behaves like an array but can grow, as required.

As the elements are added to an ArrayList object, the capacity is automatically increased. An ArrayList object initially allocates 16 elements. When we add a seventeenth element, the ArrayList expands to 32 elements.

How to access the elements in an ArrayList

We can access the elements in an ArrayList object in the same way as you access the arrays. We can also use ArrayList methods to add the elements to or remove the elements from an ArrayList.

To decrease the capacity of an ArrayList, we can call TrimToSize method.

Use Add method to add the items to an ArrayList.

We can also do Foreach to iterate over the items in an ArrayList.

Note

ArrayList elements are the objects such as System. Object. Thus, when we retrieve the elements from the list, we must do some conversions.

Types of methods used in an ArrayList

Add

Add an object at the end of the ArrayList.

Remove

Remove the first appeared object from the ArrayList.

Clear

Removes all the elements from the ArrayList.

Insert

Inserts an element into the ArrayList at the specified index.

TrimToSize

Sets the capacity to the actual number of the elements in the ArrayList.

Sort

Sorts the elements in the ArrayList.

Reverse

Reverse the elements in the ArrayList.

The sample code given shows how to use an ArrayList.

  • We must include the system.collection namespace, as shown below.
    1. using System;  
    2. using System.Collections;  
  • The ArrayList is initialized without specifying the size, because it may grow, when needed.

Name of Arraylist is theAnimals, as shown below.

Code 

  1. public class Zoo  
  2.     {  
  3.         private ArrayList theAnimals;  
  4.         public ArrayList ZooAnimals  
  5.         {  
  6.             get  
  7.             {  
  8.                 return theAnimals;  
  9.             }  
  10.         }  
  11.   
  12.   
  13.         public Animal this[int i]  
  14.         {  
  15.             get  
  16.             {  
  17.                 return (Animal) theAnimals[i];  
  18.             }  
  19.             set {  
  20.                 theAnimals[i] = value;  
  21.             }  
  22.         }  
  23.         public Zoo()  
  24.         {  
  25.             theAnimals = new ArrayList();  
  26.         }  
  27.     }   
  • The Add and Insert methods are used to add the elements to the array. This is the main difference between an Array and an ArrayList, as shown below.
    1. public class Guardian  
    2.     {  
    3.         static void Main(string[] args)  
    4.         {  
    5.             Zoo myzoo = new Zoo();  
    6.             myzoo.ZooAnimals.Add(new Lion());  
    7.             myzoo.ZooAnimals.Add(new Elephant());  
    8.             myzoo.ZooAnimals.Insert(1, new Lion());  
    9.   
    10.             Animal a = myzoo [0];  
    11.             myzoo[1] = new Tiger();  
    12.   
    13.         }  
    14.   
    15.     }  
  • We can access the elements of the ArrayList by using the Index Operator[], as shown below.
    1. Animal a = myzoo[0];  
    2.   myzoo [1] = new Tiger();  

            public abstract class Animal{

            abstract public void Eat(); 
               } 
 

            public  class Lion :Animal{

               public override void Eat();
            }
 

              public class Elephant :Animal{

                  public override void Eat();
               }   
 

               public class Tiger :Animal{

                  public override void Eat();
               }
 
 
Thanks for reading my article. I hope you like it.