ArrayList

Learn to use a dynamically-sized collection instead of arrays.

Introduction

When we need to store multiple items in one place, we can use arrays. We can use them to store a number items of the same type:

int[] points = new int[30]; // Array that can hold 30 integers

We can also iterate an array to process one item at a time:

for (int i  = 0; i < points.Length; i++)
{
	Console.WriteLine(points[i]);
}

Arrays are great, but they all suffer from one major shortcoming. That is,we have to know how many items our array will hold before we create and start using it. Arrays aren’t dynamic. In a real-life application, though, we may not know how many items we plan to store.

The ArrayList class

The ArrayList class was introduced as a solution to the aforementioned issue. It’s a class that can dynamically increase its size to accommodate more items. It’s also much easier to interact with than arrays. We don’t have to provide an index to add an item to the list.

Let’s see several common members of this class:

Get hands-on with 1200+ tech skills courses.