Arrays are useful for when your data is of a fixed size. But when you have a need for a data structure that grows over time you can make use of an array list. An array list is a structure built using an array but has the ability to change it sizes as items are added and removed.
This structure is particularly handy in programs using databases as you never know how many results you will get back from a select query.
Creating an Array List
To create an array list you simply write a statement like this:
ArrayList items = new ArrayList();
This creates an ArrayList that can hold items of any type. You can add integers, doubles, chars, strings, anything you want. It is important to note that if you add a primitive type to the ArrayList it is actually converted to an equivalent object wrapper.
Note: the ArrayList class does not support the use of generics. If this is something you want look at the List class instead.
Adding Items
To add items we can use the Add method:
items.Add(3); items.Add(“codecall”);
We can also add an entire collection to the end of a list using the AddRange function:
items.AddRange(new []{1,3,2,4});
Note: there is no function to add a collection to the front of a list. However, this is possible by making a second list with just the elements you want at the front and then adding the original list to the end of that list as follows:
ArrayList items = new ArrayList();
items.Add(5);
items.Add(3);
items.AddRange(new []{1,3,2,4});
var items2 = new ArrayList() {0, 0, 0};
// add items2 to the front of items
items2.AddRange(items);
items = items2;
foreach (var itms in items)
Console.WriteLine(itms);
We add all of the elements in items to the end of items2. So now items2 has the representation {0, 0, 0, 5, 3, 1, 3, 2, 4} and items has the representation {5, 3, 1, 3, 2, 4}. This, however, is not the result we want. We want items to have the representation {0, 0,0, 5, 3, 1, 3, 2, 4}. This is why we set the reference of items to be the same as items2. Now the foreach loop will output the contents of items as {0, 0, 0, 5, 3, 1, 3, 2, 4} as we wanted.
Removing Items
There are three ways of removing items from an array list:
- Remove
- RemoveAt
- RemoveRange
Example:
items.Remove(3);
This removes the first occurrence of 3 from items and the list becomes {0, 0, 0, 5, 1, 3, 2, 4}. If I call Remove with a parameter that does not exist the function simply does nothing.
RemoveAt takes an index and removes the element at the index.
Example:
Items.RemoveAt(0); // removes first element in the array list
It is important remember that array lists start at index 0 not 1 just like arrays.
This function DOES NOT check for out of bounds errors... so to be perfectly safe you should include a check to make sure the index is in the bounds of the array before calling the function.
RemoveRange takes two parameters: the index of the first element to remove followed by the number of elements to remove from the collection. Like RemoveAt this function does not do bounds checking.
As usual, any questions feel free to ask. :)


Sign In
Create Account


Back to top









