08 Aug C# ArrayList
The ArrayList Collection implements the IList Interface. The size of an ArrayList in C# can be modified i.e., you can easily add or remove elements. This is not the case with Arrays in C#. Once created, you cannot modify the C# Arrays. ArrayList can have duplicate elements. The ArrayList class is defined in the System.Collections namespace:
1 2 3 |
using System.Collections; |
Create an ArrayList
Let us see how to create an ArrayList in C#. Use the new keyword to create a new ArrayList:
1 2 3 |
ArrayList arrList = new ArrayList(); |
Let us see how to create an ArrayList with integer elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(9); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } } } } |
Output
1 2 3 |
ArrayList: 10 29 12 9 35 |
Count the number of ArrayList elements
To count the number of elements an ArrayList i.e., how many elements it has, use the count property. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(9); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } Console.WriteLine("\nCount = " + arrList.Count); } } } |
Output
1 2 3 4 |
ArrayList: 10 29 12 9 35 Count = 5 |
Insert Elements in an ArrayList
To insert elements at a specific index in an ArrayList, use the Insert() method. Under the parameter, set the index number where the element needs to be inserted and the element itself:
1 2 3 |
arrList.Insert(2, 100); |
Above, we are inserting an element 100 at index 2.
Let us see an example of inserting elements in an ArrayList:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(9); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } // Count the number of elements Console.WriteLine("\nCount = " + arrList.Count); // Insert an element at a specified index arrList.Insert(2, 100); // Display the updated ArrayList Console.Write("\nArrayList (Updated): "); foreach (int i in arrList) { Console.Write(i + " "); } // Display the count after inserting an element Console.WriteLine("\nUpdated Count = " + arrList.Count); } } } |
Output
1 2 3 4 5 6 7 |
ArrayList: 10 29 12 9 35 Count = 5 ArrayList (Updated): 10 29 100 12 9 35 Updated Count = 6 |
Remove an Element in an ArrayList
To remove an element from the ArrayList, use the Remove() method and mention the element to be removed as a parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(9); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } // Count the number of elements Console.WriteLine("\nCount = " + arrList.Count); // Remove the element 29 arrList.Remove(29); // Display the updated ArrayList Console.Write("\nArrayList (Updated): "); foreach (int i in arrList) { Console.Write(i + " "); } // Display the count after inserting an element Console.WriteLine("\nUpdated Count = " + arrList.Count); } } } |
Output
1 2 3 4 5 6 7 |
ArrayList: 10 29 12 9 35 Count = 5 ArrayList (Updated): 10 12 9 35 Updated Count = 4 |
Sort an ArrayList
To sort an ArrayList, use the Sort() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(9); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } // Sort the ArrayList arrList.Sort(); // Display the updated ArrayList Console.Write("\nSorted ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } } } } |
Output
1 2 3 4 |
ArrayList: 10 29 12 9 35 Sorted ArrayList: 9 10 12 29 35 |
Fetch an ArrayList element
To fetch an element at a specific position in an ArrayList, use the index of the element. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(90); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } // Fetch an element located at index 3rd i.e. position 4th Console.Write("\n4th Element = "+arrList[3]); } } } |
Output
1 2 3 4 |
ArrayList: 10 29 12 90 35 4th Element = 90 |
Check for the existence of an ArrayList element
Use the contains() method to check for the existence of an element in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arrList = new ArrayList(); // Add integer elements arrList.Add(10); arrList.Add(29); arrList.Add(12); arrList.Add(90); arrList.Add(35); // Display the ArrayList Console.Write("ArrayList: "); foreach (int i in arrList) { Console.Write(i + " "); } // Does the ArrayList contain the element 29? Console.Write("\nContains an element 29? "+arrList.Contains(29)); } } } |
Output
1 2 3 4 |
ArrayList: 10 29 12 90 35 Contains an element 29? True |
Read More
No Comments