Coding With Flavio
Lesson Summary:
Lists
A List is a powerful data structure that has quite a bit of overlap with the array (in fact, a List is built ontop of an array). Unlike the basic array, a List will start with a size of 0 and will adjust its size as you add and remove things to and from it.
To create an empty list of integers, you can use the following notation:
List<int> myList = new List<int>();
Note the triangle braces containing the type, <int>. We will cover this notation more in the far future, when we talk about generics. Also note the use of the operator new and the empty parentheses at the end, ().
To add an item to the list, simply use the method Add:
myList.Add(5);
You can index from the list just like you did for the array, but keep in mind the size boundaries:
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(7);
int myValue1 = myList[0]; // value will be 5
int myValue2 = myList[1]; // value will be 7
int crash1 = myList[-1]; // will crash; 0 is lowest index
int crash2 = myList[2]; // will crash; list only has 2 items so far
Notice that in the example above, the values are added in first-in-first-out (FIFO) order. That is, the first element in the list, (myList[0}) will be the first to be added using Add. Also note that, unlike with the array (which has a constant length), the number of elements in the list starts at 0 and expands as you add more elements.
Like the array's Length property, you can get the number of items in your list by using the Count property:
List<int> myList = new List<int>();
int listSize1 = myList.Count; // will be 0
myList.Add(5);
myList.Add(7);
int listSize2 = myList.Count; // will be 2
Other methods of Lists
Lists have many useful methods. I will cover some here, but not all.
Remove:
You can remove an item from a list by using Remove. This will remove the first item with a specific value:
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(7);
myList.Remove(5);
In this example, the list will only have 1 value in, it, which is 7. myList.Count will be 1.
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(7);
myList.Add(5);
myList.Remove(5);
Note that this example will only remove the first 5 it runs into, which is the first one. The list will have 2 values: 7 and 5, consecutively.
Remove At:
Like remove, but you specify an index instead of a value:
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(6);
myList.Add(7);
myList.RemoveAt(1); // Removes 6 from the list
Keep in mind that the indices change as items are removed from the list!
Insert:
You can insert into the middle of the list:
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(7);
myList.Insert(1, 6); // Inserts 6 between the 5 and 7. Values will be 5, 6, 7, consecutively
IndexOf:
Gets the first index to have the specified value. Returns -1 if the list does not contain the value.
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(6);
myList.Add(7);
int indexOf7 = myList.IndexOf(7); // Value will be 2
int indexOf9 = myList.IndexOf(9); // Value will be -1