In the last topic we talked about the Array. It was mentioned that an Array was a fixed size and fixed typed. The ArrayList is classified as a dynamic array. It implements the IList interface which simplifies add, insert, and remove commands. ArrayList is typically thought of as old school and it slowly being replaced with a regular List<T> data structure.
Add: pretty standard list style add function.
var arrayList1 = new ArrayList(); arrayList1.Add(1); arrayList1.Add("quick"); arrayList1.Add("brown");
Insert: On the insert we need to pass in the position where the insert is to happen. If you notice we do not need to insert new values add the end of the array.
arrayList1.Insert(5,"fox"); arrayList1.Insert(6, "jumps"); arrayList1.Insert(7, "over"); arrayList1.Insert(8, "the"); arrayList1.Insert(9, "lazy"); arrayList1.Insert(10, "dog");
arrayList1.Insert(11, "cat");
arrayList1.Insert(3, "the");
Remove: This will scan the ArrayList and remove the item
arrayList1.Remove("cat");
RemoveAt: Allows us to remove an item by index
arrayList1.RemoveAt(2);
The one setback of an ArrayList is that in order to get any value out of it, the item needs to have a cast assigned.
var value = (int)arrayList1[0] + (int)arrayList1[1]; string stringValue = string.Empty; for (int i = 2; i < arrayList1.Count; i++) { stringValue = (string)arrayList1[i]; }