More List Operations4:45 with Jeremy McLain
Let's take a closer look at how to use the List class.
So far we've seen how to create a list, add items to it and 0:00 access those items using their index. 0:04 We can add items to the list at the time it's created by using a collection 0:06 initializer. 0:10 We simply instantiate our list exactly as we had before. 0:11 Before typing the semi-colon we add opening and closing curly braces here. 0:18 We still need the ending semicolon though. 0:25 Now we can list the items we want our list to start out with just like we did with 0:28 arrays. 0:32 So I'll say Sue. 0:32 Bill. 0:36 Allen. 0:41 Beth. 0:45 And Mary. 0:48 Notice that even though we've only listed five items here, 0:52 the capacity of the list is eight. 0:55 This is because the collection initializer actually just calls the list add method. 0:57 This is exactly like creating the list and then calling the add method five times. 1:03 The list had two resize itself to a capacity of eight 1:08 when it got to the fifth item. 1:12 Again this is a good time to give the list an initial capacity and 1:14 avoid some overhead. 1:17 [BLANK AUDIO] So we can say five right here. 1:18 Another way to instantiate a list is with another collection 1:26 by passing it to the constructor. 1:30 So let's create another list called students2 and 1:32 we'll say new list string. 1:37 And we'll pass in the students list we created before. 1:42 Passing in another list essentially makes a copy of the list. 1:48 I could have passed in any collection type here, in fact, 1:55 passing in an array here is a good way to convert an array into a list. 1:59 We can convert a list to an array by calling two array. 2:05 So let's create a string array here. 2:09 Call it studentArray. 2:12 And set equal to students.Toarray. 2:17 We can iterate through all of the items in a list the same way we did with 2:27 the arrays. 2:31 We will use a foreach loop but we could use a for 2:31 loop for a while loop so say foreach string student in students. 2:37 And then we'll just print them to the console. 2:48 So I'll say console.writeline student. 2:50 We can also insert items anywhere in the list using the Insert method. 3:00 So I'll say students.Insert. 3:06 And here we specify the index that we want to insert the new item at so 3:10 I'll put something right it index 1. 3:15 And will insert Frank. 3:19 When we enter items into a list all of the items after where we inserted 3:25 the item get moved up. 3:30 So Bill, Allen, Beth, and Mary all had to be moved up. 3:32 This is not a quick task if there are a lot of items. 3:37 Also if there isn't enough room to add the item, then the list is resized. 3:40 There are a couple of ways to remove items from a list. 3:45 If we know the index of the item that we want to remove, 3:49 then we can use the remove at method. 3:51 Let's say Bill moved to another school so we need to remove him from the list. 3:53 If we happen to already know that Bill is at index 2, 3:58 we can say students.removeAt 2. 4:05 Now if we take a look at students, we can see that Bill is gone and 4:10 all of the items after where Bill was have been shifted back down in the list. 4:15 Like arrays, lists are not optimized to have items inserted into or 4:20 removed from the middle of them. 4:24 However, this usually isn't a big problem. 4:26 You'll find that inserting and deleting items from the middle of a list 4:29 is actually not as common in everyday programs as you might think. 4:33 There are collections that are designed to make adding and 4:36 removing items much faster though. 4:39 I'll mention some of these more specialized collections near the end of 4:41 this course. 4:44
You need to sign up for Treehouse in order to download course files.Sign up