1 00:00:00,350 --> 00:00:04,420 So far we've seen how to create a list, add items to it and 2 00:00:04,420 --> 00:00:06,890 access those items using their index. 3 00:00:06,890 --> 00:00:10,660 We can add items to the list at the time it's created by using a collection 4 00:00:10,660 --> 00:00:11,880 initializer. 5 00:00:11,880 --> 00:00:15,000 We simply instantiate our list exactly as we had before. 6 00:00:18,530 --> 00:00:23,380 Before typing the semi-colon we add opening and closing curly braces here. 7 00:00:25,150 --> 00:00:26,850 We still need the ending semicolon though. 8 00:00:28,500 --> 00:00:32,449 Now we can list the items we want our list to start out with just like we did with 9 00:00:32,449 --> 00:00:32,962 arrays. 10 00:00:32,962 --> 00:00:34,452 So I'll say Sue. 11 00:00:36,764 --> 00:00:37,972 Bill. 12 00:00:41,188 --> 00:00:42,358 Allen. 13 00:00:45,302 --> 00:00:46,276 Beth. 14 00:00:48,132 --> 00:00:52,030 And Mary. 15 00:00:52,030 --> 00:00:55,710 Notice that even though we've only listed five items here, 16 00:00:55,710 --> 00:00:57,930 the capacity of the list is eight. 17 00:00:57,930 --> 00:01:03,590 This is because the collection initializer actually just calls the list add method. 18 00:01:03,590 --> 00:01:08,480 This is exactly like creating the list and then calling the add method five times. 19 00:01:08,480 --> 00:01:12,280 The list had two resize itself to a capacity of eight 20 00:01:12,280 --> 00:01:14,250 when it got to the fifth item. 21 00:01:14,250 --> 00:01:17,940 Again this is a good time to give the list an initial capacity and 22 00:01:17,940 --> 00:01:18,702 avoid some overhead. 23 00:01:18,702 --> 00:01:24,080 [BLANK AUDIO] So we can say five right here. 24 00:01:26,790 --> 00:01:30,420 Another way to instantiate a list is with another collection 25 00:01:30,420 --> 00:01:32,560 by passing it to the constructor. 26 00:01:32,560 --> 00:01:37,655 So let's create another list called students2 and 27 00:01:37,655 --> 00:01:40,440 we'll say new list string. 28 00:01:42,230 --> 00:01:45,300 And we'll pass in the students list we created before. 29 00:01:48,218 --> 00:01:52,500 Passing in another list essentially makes a copy of the list. 30 00:01:55,999 --> 00:01:59,808 I could have passed in any collection type here, in fact, 31 00:01:59,808 --> 00:02:05,240 passing in an array here is a good way to convert an array into a list. 32 00:02:05,240 --> 00:02:09,790 We can convert a list to an array by calling two array. 33 00:02:09,790 --> 00:02:12,470 So let's create a string array here. 34 00:02:12,470 --> 00:02:14,920 Call it studentArray. 35 00:02:17,150 --> 00:02:22,114 And set equal to students.Toarray. 36 00:02:27,637 --> 00:02:31,259 We can iterate through all of the items in a list the same way we did with 37 00:02:31,259 --> 00:02:31,980 the arrays. 38 00:02:31,980 --> 00:02:37,059 We will use a foreach loop but we could use a for 39 00:02:37,059 --> 00:02:44,290 loop for a while loop so say foreach string student in students. 40 00:02:48,850 --> 00:02:50,430 And then we'll just print them to the console. 41 00:02:50,430 --> 00:02:54,573 So I'll say console.writeline student. 42 00:03:00,910 --> 00:03:06,140 We can also insert items anywhere in the list using the Insert method. 43 00:03:06,140 --> 00:03:08,040 So I'll say students.Insert. 44 00:03:10,620 --> 00:03:15,370 And here we specify the index that we want to insert the new item at so 45 00:03:15,370 --> 00:03:18,135 I'll put something right it index 1. 46 00:03:19,670 --> 00:03:20,610 And will insert Frank. 47 00:03:25,350 --> 00:03:30,049 When we enter items into a list all of the items after where we inserted 48 00:03:30,049 --> 00:03:32,100 the item get moved up. 49 00:03:32,100 --> 00:03:37,210 So Bill, Allen, Beth, and Mary all had to be moved up. 50 00:03:37,210 --> 00:03:40,920 This is not a quick task if there are a lot of items. 51 00:03:40,920 --> 00:03:45,720 Also if there isn't enough room to add the item, then the list is resized. 52 00:03:45,720 --> 00:03:49,010 There are a couple of ways to remove items from a list. 53 00:03:49,010 --> 00:03:51,945 If we know the index of the item that we want to remove, 54 00:03:51,945 --> 00:03:53,986 then we can use the remove at method. 55 00:03:53,986 --> 00:03:58,850 Let's say Bill moved to another school so we need to remove him from the list. 56 00:03:58,850 --> 00:04:05,898 If we happen to already know that Bill is at index 2, 57 00:04:05,898 --> 00:04:10,730 we can say students.removeAt 2. 58 00:04:10,730 --> 00:04:15,330 Now if we take a look at students, we can see that Bill is gone and 59 00:04:15,330 --> 00:04:20,440 all of the items after where Bill was have been shifted back down in the list. 60 00:04:20,440 --> 00:04:24,950 Like arrays, lists are not optimized to have items inserted into or 61 00:04:24,950 --> 00:04:26,880 removed from the middle of them. 62 00:04:26,880 --> 00:04:29,620 However, this usually isn't a big problem. 63 00:04:29,620 --> 00:04:33,100 You'll find that inserting and deleting items from the middle of a list 64 00:04:33,100 --> 00:04:36,650 is actually not as common in everyday programs as you might think. 65 00:04:36,650 --> 00:04:39,100 There are collections that are designed to make adding and 66 00:04:39,100 --> 00:04:41,660 removing items much faster though. 67 00:04:41,660 --> 00:04:44,790 I'll mention some of these more specialized collections near the end of 68 00:04:44,790 --> 00:04:45,310 this course.