TL ; DR:
Insert(i, x) puts x at index i. Use RemoveAt(i) to take out the item at index i. The other items move to make room or to close the gap.
List<string> colors = new List<string>(new[] {"red", "green", "blue"});
colors.Insert(1, "pink");
// colors is now [red, pink, green, blue]
colors.RemoveAt(0);
// colors is now [pink, green, blue]
Full lesson:
Add puts an item at the end. To put one anywhere else in a List, use Insert(i, x). The items from index i on move one place to the right.
List<string> pets = new List<string>(new[] {"Rex", "Milo"});
pets.Insert(0, "Luna");
// pets is now [Luna, Rex, Milo]
You met RemoveAt(i) in the last lesson. It takes out the item at index i. The items after it move one place to the left.
pets.RemoveAt(0);
// pets is now [Rex, Milo]
Watch the indexes. After an insert or a removal, the items behind that spot have new indexes.
Don't mix up RemoveAt and Remove. Remove(x) takes out the first item equal to x. It doesn't take an index.
Your task
pets holds the pets waiting at the vet, in order.
Bella has an appointment, so she goes second, at index 1. Then Milo goes home. Take him out of the line.
The code below prints how many pets wait once Bella is in. At the end, it prints the pets left.
Expected output:
4
Rex Bella LunaFree plan: 30 tutor messages. PRO is unlimited.
