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:
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:
ArrayList arrList = new ArrayList();
Let us see how to create an ArrayList with integer elements:
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
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:
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
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:
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:
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
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:
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
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:
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
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:
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
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:
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
ArrayList: 10 29 12 90 35 Contains an element 29? True
Read More
No Comments