ArrayList is a dynamic array. What that means is that an ArrayList can have any amount of objects and of any type. This data structure was originally designed to simplify the processes of adding new elements into an array.
Under the hood, an ArrayList is an array whose size is doubled every time it runs out of space. Doubling the size of the internal array is a very effective strategy, that reduces the amount of element-copying in the long run. I won't get into the proof of that here.
ArrayList is a collection that is best avoided. But it is often used in older legacy programs - so it must be supported. Newer .NET Framework versions offer better collections, like Lists, of which we will learn in the future.
Additional Information
The reason why ArrayList even exists is because Microsoft decided to launch C# 1.0 in a rush, back in January 2002, as a direct competitor to Java. So much so, that they decided to launch it without generics and a few other crucial elements of a language, which led to a temporary workaround of boxing and unboxing values to and from object, like ArrayList does.
The data structure is very simple to use:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add(56);
myArrayList.Add("String");
myArrayList.Add(new Window());
Console.ReadLine();
}
}
}
|
The downside to the ArrayList data structure is that you must cast the retrieved values back into their original type:
|
1
|
int arrayListValue = (int)myArrayList[0];
|
The ArrayList has following important methods:
Add() - appends a new element object to the end. We can keep adding elements to the collection until memory runs out. Example:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
using
System.Collections;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Create an ArrayList and add 3 elements
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
Console.ReadLine();
}
}
}
|
AddRange() - combines two ArrayLists. Internally, AddRange() uses the Array.Copy() or CopyTo() methods, which have better performance than some loops. 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
|
using System;
using System.Collections;
namespace HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Create an ArrayList with two values
ArrayList list = new ArrayList();
list.Add(5);
list.Add(7);
// Create a second ArrayList
ArrayList list2 = new ArrayList();
list2.Add(10);
list2.Add(13);
// Add second ArrayList to first
list.AddRange(list2);
// Display the values
foreach (int i in list);
Console.WriteLine(i);
Console.ReadLine();
}
}
}
|
Clear() - clears the elements of an ArrayList. Internally, this calls the Array.Clear() method, which I explained in the previous lesson. 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
|
using System;
using System.Collections;
namespace HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Create an ArrayList with two values
ArrayList list = new ArrayList();
list.Add(9);
list.Add(10);
// Show number of elements in ArrayList
Console.WriteLine(list.Count);
// Clear the ArrayList
list.Clear();
// Show count again
Console.WriteLine(list.Count);
Console.ReadLine();
}
}
}
|
Sort(), Reverse() - these methods do exactly what their name says: they sort an ArrayList, or reverse the order of their elements. 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
|
using System;
using System.Collections;
namespace HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Create an ArrayList with four values
ArrayList list = new ArrayList();
list.Add("Cat");
list.Add("Zebra");
list.Add("Dog");
list.Add("Cow");
// Sort the ArrayList
list.Sort();
// Display the ArrayList elements
foreach (int value in list)
Console.WriteLine(value);
// Reverse the ArrayList
list.Reverse();
// Display the ArrayList elements again
foreach (int value in list)
Console.WriteLine(value);
Console.ReadLine();
}
}
}
|
Insert(), RemoveAt(), RemoveRange() - methods used to add or remove elements. Remove() uses a numeric index to specify which element you want to remove. RemoveRange() specifies a starting index from which it should start removing elements, and another numeric parameter, to specify how many elements should be removed. 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 HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Create an ArrayList with three values
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
// Remove middle element in ArrayList
list.RemoveAt(1); // It becomes [One, Three]
// Insert word at the beginning of ArrayList
list.Insert(0, "Carrot"); // It becomes [Carrot, One, Three]
// Remove first two words from ArrayList
list.RemoveRange(0, 2);
// Display the result ArrayList
foreach (int value in list)
Console.WriteLine(value);
Console.ReadLine();
}
}
}
|
The most important property of ArrayLists is Count:
Count - returns the number of elements in an ArrayLists.