Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Lists7:14 with Jeremy McLain
The List collection type is much easier to use than an array, however, it has the same performance limitations.
[MUSIC] 0:00 In the previous video we learned that arrays can be unwieldy when it comes to 0:04 adding and removing elements. 0:09 The list collection type, on the other hand, is much easier to use. 0:12 Adding or removing elements in a list is as easy as calling a method. 0:15 We can find the list collection type in the System.Collections.Generic namespace. 0:20 It's a very commonly used namespace. 0:25 So the C# REPL already has a using directive for it. 0:27 Normally we'd want to put using 0:31 System.Collections.Generic at the top of our file. 0:34 But because we're using the C# REPL we don't need to do this here. 0:40 Let's create a list of student names. 0:45 To create a list just type List and then an opening angle bracket. 0:48 Here we put the type of the items we'd like our list to contain. 0:54 So in this case, it'll be string. 0:57 Now we need to give our list variable a name. 1:01 So we'll say student or students. 1:03 students is just a variable that can hold a list object. 1:08 We still need to instantiate a list. 1:12 So we will say = new List<string>. 1:15 And then our parentheses. 1:20 The list class is a special type of class called a generic. 1:22 This allows the list class to be a collection of any type of object. 1:27 We simply specify the type of object that we want the list to contain 1:31 here right in between the angle brackets. 1:36 We read this as a list of strings. 1:39 Generics are not specific to collection types. 1:42 We can create generic classes that aren't collections. 1:46 That's a topic for another time though. 1:49 Notice that we don't have to specify how large the list is. 1:51 We can just go ahead and add items to the list and it will grow as needed. 1:56 To add an item to the list, we call the add method. 2:02 So I'll say students.Add and we'll add Sue. 2:05 If we inspect the students list in the REPL we'll see that 2:12 it now contains one item. 2:17 Let's add a few more student names. 2:19 So I'll say students.Add("Bill"); and 2:22 students.Add("Allen");. 2:28 The add method appends items to the end of the list. 2:35 We can see how many items are in the list by calling the Count property. 2:38 So we'll say students.Count. 2:42 Notice that we use the Count property instead of length like we did with arrays. 2:47 We can index into a list the same way we did with arrays though. 2:52 So students at index 0, at index 1. 2:56 And we can get the last item in the list by using Count-1. 3:03 So we'll say, students[students.Count-1];. 3:06 Notice that the items stay in the order in which they're added. 3:14 So what's happening here? 3:17 You might be wondering how we're able to add items to the list. 3:19 And the list just seems to grow to the size we need. 3:23 The list collection has another property that gives us a clue as to 3:25 what's happening. 3:29 Let's call the capacity property on our list. 3:30 So I'll say students.Capacity. 3:33 Hm. 3:37 We've only added three items to the list but 3:38 this says that the list has a capacity of four. 3:41 The truth is the list collection is actually just a wrapper around an array. 3:45 Underneath the hood our items are all stored in an array. 3:50 When we first declare a list, the array is null. 3:55 The first time we add an item to the list, an array of length 3:58 four is created to store that item and up to three future items. 4:01 This is why the capacity of our list is four, 4:06 even though we've only added three items. 4:09 Let's see what happens when we add a fourth and then a fifth item to the list. 4:12 So I'll say 4:16 students.Add("Beth"); and 4:19 Mary. 4:28 Now when we check the count, we'll get five. 4:30 But when we check the capacity, We get eight. 4:36 The list has doubled in capacity from four to eight. 4:42 When we added the fifth item, there wasn't enough capacity to store the item. 4:46 So instead, it created a new a array of length eight. 4:51 It then copied all of the items from the original array to this new larger array 4:56 and then added them. 5:01 Each time the list runs out of room in the underlying array, 5:02 it creates a new array that's twice the size of the original array. 5:06 And copies over all the items. 5:10 If we were working directly with an array we'd have to do this work of growing 5:12 the array ourselves. 5:16 A list essentially works the same way an array does but it's easier to use. 5:17 We can just add items to the list without thinking about its capacity. 5:23 Lists aren't necessarily more efficient than arrays in terms of performance. 5:27 Setting and retrieving values from a list is still relatively fast. 5:32 However, adding and removing items from a list can be very slow. 5:36 It's a good practice to first estimate how large the list will need to be 5:40 in order to contain all of the items. 5:44 And then instantiate a list that's at least that large. 5:46 We can pass the capacity to the list constructor 5:50 to tell it how large to make the underlying array. 5:53 So we can say List<string> students = new List<string>. 5:56 And then we can pass in 500 here. 6:05 Or anything we want. 6:12 Now that we have a list with the capacity of 500 adding items takes little 6:17 extra overhead. 6:22 If your estimate is too low, don't worry. 6:23 All that means is the list will need to grow, but 6:26 this will be handled automatically for you. 6:28 The fact that the underlying array is encapsulated 6:31 inside of the list object is another huge benefit. 6:35 This solves the object reference problem that we discussed earlier. 6:38 Other parts of the program don't have a reference to the underlying array object. 6:42 Instead, they have a reference to the list object. 6:46 So the underlying array can be swapped out as often as we want. 6:50 And we don't have to worry about updating all the other 6:54 variables that are referring to the list. 6:57 Because lists behave so similarly to arrays, 6:59 they can be used in almost every situation where we would use arrays. 7:02 The advantage of using a list is in their ease of use. 7:07 This makes them the most common collection type used in programming. 7:10
You need to sign up for Treehouse in order to download course files.Sign up