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. We won’t get into the proof of that here. The data structure is very simple to use:
1 2 3 4 5 |
ArrayList myArrayList = new ArrayList(); myArrayList.Add(56); myArrayList.Add("String"); myArrayList.Add(new Window()); |
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() – This 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 |
using System.Collections; class Program { static void Main() { // Create an ArrayList and add 3 elements. ArrayList list = new ArrayList(); list.Add("One"); list.Add("Two"); list.Add("Three"); } } |
AddRange() – we can use this method to combine two ArrayLists. Internally, AddRange() uses the Array.Copy() or CopyTo() methods, which have better performance than some loops. Here is how we use it:
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 |
using System; using System.Collections; class Program { static void Main() { // 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); } } |
Clear() – You can call the instance method Clear() on your 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 |
using System; using System.Collections; class Program { static void Main() { // 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); } } |
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 |
using System; using System.Collections; class Program { static void Main() { // Create an ArrayList with four strings. 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 (string value in list) Console.WriteLine(value); // Reverse the ArrayList. list.Reverse(); // Display the ArrayList elements again. foreach (string value in list) Console.WriteLine(value); } } |
Insert(), RemoveAt(), RemoveRange() – methods used to add or remove elements into an ArrayList. Remove() it 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 |
using System; using System.Collections; class Program { static void Main() { // Create an ArrayList with three strings. 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 (string value in list) Console.WriteLine(value); } } |
The most important property of ArrayLists is Count:
Count – returns the number of elements in an ArrayList.
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.
Tags: arraylist, arrays, data structures