Array List

What is Array List?

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automatically as you add items in it. Unlike an array, you don't need to specify the size of ArrayList.

the ArrayList class implements IEnumerable, ICollection, and IList interfaces. So, you can create an object of ArrayList class and assign it to the variable of any base interface type. However, if you assign it to IEnumerable or ICollection type variable then you won't be able to add elements and access ArrayList by index.

For Ex:

     class CollectionExamples
    {
        static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();//can add elements with additional methods
            IList _ArrayIList = new ArrayList();//can add elements
            ICollection _ArrayICollection = new ArrayList();//can not add elements
            IEnumerable _ArrayIEnumerable = new ArrayList();//can not add elements

      
        }
    }

From above example you can see that using ArrayList is more useful compare to others.because ArrayList class contains some additional methods which are not the members of IList interface such as AddRange(), BinarySearch(), Repeat(), Reverse(), etc.

Properties of ArrayList

  1. Capicity: Gets or sets the number of elements that the ArrayList can contain
  2. Count:Gets the number of elements actually contained in the ArrayList.
  3. IsFixedSize: Gets a value indicating whether the ArrayList has a fixed size.
  4. IsReadOnly: Gets a value indicating whether the ArrayList is read-only.
  5. Item:Gets or sets the element at the specified index.

Methods Of ArrayList

1.Add()/AddRange: Add() method adds single elements at the end of ArrayList.
AddRange() method adds all the elements from the specified collection into ArrayList

 Example:

class CollectionExamples
    {
        static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();
            ArrayList _ArrayList2 = new ArrayList();
            _ArrayList.Add(1);
            _ArrayList.Add("Two");
            _ArrayList.Add(2.5);
            _ArrayList.Add(3.5f);

            _ArrayList2.AddRange(_ArrayList);//Adding entire collection

            Console.WriteLine("Array List 1 Count {0}", _ArrayList.Count);
            Console.WriteLine("Array Lisr 2 Count {0}", _ArrayList2.Count);
            Console.ReadLine();

        }
    }
                                                                                                                                                            




2.Insert()/InsertRange():

Insert() method insert a single elements at the specified index in ArrayList.
InsertRange() method insert all the elements of the specified collection starting from specified index in ArrayList.       

Example:   

 class CollectionExamples
    {
        static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();
            ArrayList _ArrayList2 = new ArrayList();

            _ArrayList.Add(1);
            _ArrayList.Add("Two");
            _ArrayList.Add(2.5);
            _ArrayList.Add(3.5f);
            _ArrayList.Insert(1, "Inserted"); // Inserted Single item at particular index

            _ArrayList2.AddRange(_ArrayList);
            _ArrayList2.InsertRange(3,_ArrayList); //Insert entire list from the particular index.

            foreach(var i in _ArrayList)
            {
                Console.WriteLine("Items in ArrayList - 1 {0}", i);
            }
            foreach (var i in _ArrayList2)
            {
                Console.WriteLine("Items in ArrayList - 2 {0}", i);
            }
            Console.WriteLine("Array List 1 Count {0}", _ArrayList.Count);
            Console.WriteLine("Array Lisr 2 Count {0}", _ArrayList2.Count);
            Console.ReadLine();

        }
    }
    
                                                                                                                                                            

3.Remove()/Remove Range():
Remove() method removes the specified element from the ArrayList.
RemoveRange() method removes a range of elements from the ArrayList.

  static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();
            ArrayList _ArrayList2 = new ArrayList();

            _ArrayList.Add(1);
            _ArrayList.Add("Two");
            _ArrayList.Add(2.5);
            _ArrayList.Add(3.5f);
            _ArrayList.Remove(1); //Remove 1 from Arraylist


            _ArrayList2.AddRange(_ArrayList);
            _ArrayList2.Add("New Values");
            _ArrayList2.RemoveRange(0,3);//Remove 3 element starting from 0th.
          

            foreach(var i in _ArrayList)
            {
                Console.WriteLine("Items in ArrayList - 1 {0}", i);
            }
            foreach (var i in _ArrayList2)
            {
                Console.WriteLine("Items in ArrayList - 2 {0}", i);
            }
            Console.WriteLine("Array List 1 Count {0}", _ArrayList.Count);
            Console.WriteLine("Array Lisr 2 Count {0}", _ArrayList2.Count);
            Console.ReadLine();

        }

                                                                                                                                                        


4.RemoveAt():

Removes the element at the specified index from the ArrayList.

Example  

static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();
            ArrayList _ArrayList2 = new ArrayList();

            _ArrayList.Add(1);
            _ArrayList.Add("Two");
            _ArrayList.Add(2.5);
            _ArrayList.Add(3.5f);
            _ArrayList.RemoveAt(2); //Remove 2 index element from Arraylist

            foreach(var i in _ArrayList)
            {
                Console.WriteLine("Items in ArrayList - 1 {0}", i);
            }
 
            Console.ReadLine();

        }
                                                                                                                                                         

5.Sort & Reverse():

The ArrayList class includes Sort() and Reverse() method. The Sort() method arranges elements in ascending order. However, all the elements should have same data type so that it can compare with default comparer otherwise it will throw runtime exception.

The Reverse() method arranges elements in reverse order. Last element at zero index and so on.

Example:

   static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();
         

            _ArrayList.Add(1);
            _ArrayList.Add(2);
            _ArrayList.Add(2);
            _ArrayList.Add(3);
            
            _ArrayList.Sort(); // Sorting
            foreach(var i in _ArrayList)
            {
                Console.WriteLine("Items in ArrayList - 1 {0}", i);
            }
            _ArrayList.Reverse(); //Reverse
            foreach (var i in _ArrayList)
            {
                Console.WriteLine("Items in ArrayList - 1 {0}", i);
            }
            Console.ReadLine();

        }                                                                                                                                                            


6.Contains

The Contains() method checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.

Example:

 static void Main(string[] args)
        {
            ArrayList _ArrayList = new ArrayList();

            _ArrayList.Add(1);
            _ArrayList.Add(2);
            _ArrayList.Add(2);
            _ArrayList.Add(3);
         
            _ArrayList.Sort();
            Console.WriteLine(_ArrayList.Contains(3)); //Print true
            Console.ReadLine();

        }


No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...