Collections in C#: ArrayList and Arrays

Introduction:

Mirosoft.Net has many namespaces for different purposes. Among them the System.Collections is a very important namespace in the programmers perceptive. While coding, we look for classes that can reduce manual operation. For instance if you want to sort the values in an array then you have to do the manual operations. Here obviously we look for the classes that can automate the sorting by just calling a method. In other words we can call this namespace as a utility namespace. Let us see the classes in this namespace.

Collection Classes:

The System.Collections namespace has many classes for the individual purpose. We are going to discuss the frequently used classes in this article.

  • ArrayList
  • BitArray
  • Stack
  • Queue
  • Comparer
  • HashTable
  • SortedList

 

ArrayList:

The ArrayList is one of the important classes in the System.Collection namespace. We can say it is the next generation of the Array in C#.

 

ArrayList is a dynamic array; it will increase the size of the storage location as required. It stores the value as object. The allocation of the ArrayList can be achieved through the TrimToSize property.

 

Methods:

1

Add

It will add the element as object in the ArrayList

2

AddRange

It will add the collections of elements in the object as individual objects in the ArrayList

3

Clear

It will clear the all objects in the ArrayList

4

BinarySearch

It will return the position of the search object as integer value.

5

Insert

It will insert the element in the specified location of the index in the ArrayList.

6

InsertRange

It will insert the elements in the object as individual objects in the specified location.

7

Remove

It will remove the given object in the first occurrence in the ArrayList.

8

RemoveAt

It will remove the object as specified in the argument.

9

RemoveRange

It will remove the set of objects in the ArrayList from the range specified.

10

Sort

It will do the sorting of the elements in the ascending order.

11

Reverse

It will arrange the elements in the reverse order in the ArrayList.

12

GetEnumerator

It will return the collection of objects in the ArrayList as enumerator.

13

Contains

It checks whether the objects exists or not.

 

Add:

This Add method is used to add an object to the ArrayList. Whatever we add element to the ArrayList then it consider as an object.

Let us see how to add element into ArrayList.

ArrayListObject.Add(element);

For example:

ArrayList oArrayList = new ArrayList();

oArrayList.Add("senthil");

oArrayList.Add("Kumar");

oArrayList.Add(99);

oArrayList.Add("@");

oArrayList.Add("55.55");

oArrayList.Add(oStack);
Here, I have added a few elements that everything considers as an object. Elements can be duplicates or you can add null into the ArrayList. The size will be expanded when you insert new elements.

Add Range:

The Add range method will add elements from other collections. Here the elements can be null, but the Add range collection cannot be null.

Consider this example for better understanding.

Stack oStack = new Stack();

oStack.Push(1);

oStack.Push(2);

oStack.Push(3);

oStack.Push(4);

 

ArrayList oArrayList = new ArrayList();

oArrayList.Add("Senthil");

oArrayList.Add("Kumar");

oArrayList.AddRange(oStack);
Here what happens, when you add the Stack object it considers as one of the element in the ArrayList. But when you use AddRange method it stores the elements in the Stack as individual elements in the ArrayList.

It adds the elements at the end of the ArrayList.

Here the output will be like the following.

Senthil  kumar 1 2 3 4

 

Clear:

Clear method is used to clear the elements in the ArrayList.

ArrayListObject.Clear();

ArrayList oArrayList = new ArrayList();

oArrayList.Add("c#");

oArrayList.Add("Java");

oArrayList.Add("PHP");

If you see the total elements in the ArrayList it is 3. You can check with the Count property.

Here I clear the ArrayList.

oArrayList.Clear();

Now count of the ArrayList is 0. However the Capacity remains the same as set.

 

BinarySearch:

The BinarySearch method is used to get the location of the object in the ArrayList.

It uses the Binary search algorithm to find the elements. It will return the element position in the ArrayList. Here the object can be a duplicate, in that case it will return the position of any one of the occurrences of the element.

Consider the following example,

ArrayList oArrayList = new ArrayList();

oArrayList.Add(1);

oArrayList.Add(3);

oArrayList.Add(5);

oArrayList.Add(7);

int iPos = oArrayList.BinarySearch(3);

Here it will return the location of the index value into the integer variable.

Insert:

The Insert method is used to insert the new element in the existing ArrayList. This is new specific feature in the ArrayList when you compare with the Array. Here it uses the LinkedList, when you insert the element then it will move the node.

The syntax of the Insert method follows.

ArrayListObject.Insert(Position,Object);

Consider this example for the better understanding.

ArrayList oArrayList = new ArrayList();

oArrayList.Add(1);

oArrayList.Add(3);

oArrayList.Add(4);

If you check the ArrayList, elements will be stored like this.

1 3 4

Let us insert a new element in the position of 2.

oArrayList.Insert(2,2);

Now we have inserted the element in the ArrayList.

After insertion, the output will be like the following.

1 2 3 4

InsertRange:

InsertRange method is used to insert the collection of the elements in the other collections from the specified index position. Here it will insert the individual object from the collection object.

ArrayListObject.InsertRange(Position,CollectionObject);

Let us see with an example.

ArrayList oArrayList = new ArrayList();
oArrayList.Add(1);
oArrayList.Add(5);
oArrayList.Add(6);

If you check the output of the ArrayList it will be like the following.

1 5 6

Now I am going to insert the collection object.

Stack oStack = new Stack();
oStack.Push(2);
oStack.Push(3);
oStack.Push(4);
oArrayList.InsertRange(2,oStack);

We have inserted the range of elements from the Stack.

Now output of the ArrayList will be like the following.

1 2 3 4 5 6

Remove:

The Remove method is used to remove the element from the ArrayList. If there are multiple objects in the same name when you try to remove, then it will remove the first occurrence in the ArrayList. In case of no elements found then it will not throw an exception and ArrayList count remains the same.

The syntax of the Remove method as given below.

ArrayList oArrayList = new ArrayList();
oArrayList.Add("Senthil");
oArrayList.Add("Kumar");
oArrayList.Add("Senthil");
oArrayList.Remove("Senthil");

Here it will remove the first element in the ArrayList.

RemoveAt:

The RemoveAt method is used to delete the specified location element in the ArrayList.

The syntax of the RemoveAt method is given below.

ArrayListObject.RemoveAt(ArrayListIndex);

Let us see an example,

ArrayList oArrayList = new ArrayList();
oArrayList.Add("Senthil");
oArrayList.Add("Kumar");
oArrayList.Add("Senthil");
oArrayList.RemoveAt(1);

Here the second element will be removed from the ArrayList.

 

RemoveRange:

The RemoveRange method is used to remove the collection of objects in the ArrayList.

It removes the range of Starting position to number of elements specified in the RemoveRange method.

ArrayListObject.RemoveRange(Starting Index, Number of objects);

Let us see an example for better understanding.

ArrayList oArrayList = new ArrayList();
For(int i=1;i<10;i++)
{
   oArrayList.Add(i);
}

Here 10 elements will be added as object in the Array List.

I am going to delete 3 elements starting from 5.

oArrayList.RemoveAt(5,3);

Now the output will be like the following.

1 2 3 4 8 9 10

Sort:

The Sort method is used to sort the objects in the ascending order. It uses the QuickSort algorithm to sort the elements in the ArrayList.

ArrayListObject.Sort();

Let us see an example.

oArrayList.Add("B");
oArrayList.Add("A");
oArrayList.Add("Z");
oArrayList.Sort();

The output of the above ArrayList will be like this.

A B Z

Reverse:

The Reverse method is used to arrange the objects in the Reverse order.

ArrayListObject.Reverse();

Let us see an example.

oArrayList.Add("A");
oArrayList.Add("B");
oArrayList.Add("C");
oArrayList.Reverse();

Before the reverse method

A B C

After the revserse method

C B A

 

GetEnumerator:

The GetEnumerator method is used to get the collection of the objects in the ArrayList as Iterator. This can be implemented with suitable interface. Howevery the ArrayList elements can be iterated through the foreach or loop statements.

Let us consider the following example code snippet.

oArrayList.Add("B");
oArrayList.Add("A");
oArrayList.Add("Z");
IEnumerator OIEnum = oArrayList.GetEnumerator();

Contains:

This method is used to check whether the element exists or not. It returns the Boolean result.

OArrayList.Contains("Senthil")

Properties in the ArrayList

S.No

Property Name

Description

1

Capcity

This property is used to set or get the size to the ArrayList.

As you know it will increase the size of the storage as much as required. Default size will be 16.

2

Count

It returns the total number of elements in the ArrayList.

3

IsFixedSize

It returns the Whether the ArrayList is fixed size or not. It returns the Boolean value.

4

IsReadOnly

It returns the Whether the ArrayList is Readyonly or not. It returns the Boolean value.

 

Advantages:

  • The ArrayList is not a specific data type storage location, it stores everything as object.
  • No need to do allocation and deallocation explicitly to store the data.
  • It has the explicit sorting methods.
  • It can insert and delete the elements in between positions in the ArrayList.
  • It can store the object as elements.

Disadvantages:

  • ArrayList is not a strongly typed one. It has to do the type casting when you retrieve the content. When we do the type casting every time, it hits the performance.
  • It uses the LinkedList storage approach, because if you insert or delete the specific position it has to forward/backward in the storage address.
  • Sometimes it leads to the runtime error. Consider an example, we store the ids of the employee in the arraylist and then we want to retrieve the element for some other operations. Obviously we need to do the type casting, that time if there is any string element then what will happen? It will throw the error.


Difference between Array and ArrayList

Array

ArrayList

Array uses the Vector array to store the elements

ArrayList uses the LinkedList to store the elements.

Size of the Array must be defined until redim used( vb)

No need to specify the storage size.

Array is a specific data type storage

ArrayList can be stored everything as object.

No need to do the type casting

Every time type casting has to do.

It will not lead to Runtime exception

It leads to the Run time error exception.

Element cannot be inserted or deleted in between.

Elements can be inserted and deleted.

There is no built in members to do ascending or descending.

ArrayList has many methods to do operation like Sort, Insert, Remove, BinarySeacrh,etc..,

 

Conclusion:

So far we have seen the ArrayList and its members and properties. I hope that this has given enough practical idea about the ArrayList. Next we are going to discuss about the BitArray in the same collection class. If you have any query or further clarifications about this ArrayList please free to post your feedback and corrections.

Up Next
    Ebook Download
    View all
    Learn
    View all