List Basics: Adding, Removing, and Accessing4:35 with Ben Deitch
In this video we'll learn the three basic functions of a list: adding items, removing items, and accessing items!
To get a better feel for how to use lists, we'll need to use them in code. 0:00 Let's create a new project. 0:04 Hit Next and take it from a template. 0:09 And let's call that project, Lists. 0:13 Then I'll full screen my screen. 0:17 And hide the side panel, then let's delete the comment. 0:20 And inside, let's create a new list variable. 0:26 And notice that before we finish typing, it's telling us something about lists. 0:30 For one, List green I tells us the list is an interface. 0:35 Also thanks to the angle brackets, we know that a list can take in a type parameter. 0:40 So instead of just making this a list of objects, 0:46 let's make it a list of strings and use Alt+Enter to import list. 0:50 Then let's name our list groceryLine and set it equal to a new list of strings. 0:56 So = new List<String>() and ;. 1:03 But, of course, since list is an interface, this won't work. 1:12 Instead, we'll need to use a class that implements the list interface. 1:16 There's a few choices for this, but 1:22 almost always you'll want to just use an ArrayList. 1:24 So let's just add Array right before List, And then use Alt+Enter to import it. 1:27 Also, note that while we can use an ArrayList on the left side as well, 1:34 we're better off leaving it as a list. 1:39 This way, later on if we ever decide to use a different implementation of 1:42 the list interface, we can just update it over here and be done with it. 1:47 Let's also use Alt+Enter to shorten up the right side. 1:51 Okay, now that we've got our grocery line, let's add a couple people to it. 1:56 On the next line, let's type groceryLine.add and 2:00 notice that we've got a few ways we can add items to a list. 2:04 We can add an item to the end of the list, add an item at a specified index. 2:10 Add a group of items, or add a group of items at a specified index. 2:15 Let's stick with the basic add function, and 2:19 pass in Jerome for the first person in line. 2:23 Then let's do this two more times, and add Beth. 2:26 And let's also add Sam. 2:35 Finally, let's print out our list and see what we've got. 2:40 Let's type sout and tab to bring up the println statement and pass in groceryLine. 2:43 Then let's run the app, And nice, 2:52 we're able to see exactly what's in the list just by printing it. 2:55 Okay, now let's say it's been something like 30 minutes, and Beth has had enough. 3:01 She's got places to be so she leaves the line. 3:07 Let's add a couple lines and then remove Beth from our groceryLine 3:10 by calling groceryLine.remove and passing in Beth. 3:16 Then let's print out our grocery line again. 3:20 And run the app. 3:25 Perfect. 3:28 We removed Beth from the line. 3:29 You can also remove items by passing in the index of the item 3:30 you'd like to remove. 3:35 So another way to remove Beth would be to pass in index 1. 3:37 And if we run the app, we get the same result. 3:43 Now that we've covered adding and removing, 3:48 let's talk about how we access the items in a list. 3:50 Unlike an array, where you put the index between square brackets, with a list, 3:53 you just call the get function and pass in the index of the item. 3:59 So if we wanted to create a new String variable named jerome, 4:03 and set it equal to Jerome, 4:08 we'd set it equal to groceryLine.get and pass in 0 for the index. 4:10 And if we print out our new string variable, Called Jerome, 4:16 we should see Jerome. 4:21 Awesome, with that out of the way, you're ready to get started using lists. 4:25 Just wait till after the next video. 4:30 There's a few more functions, that you really should know about 4:32
You need to sign up for Treehouse in order to download course files.Sign up