List Basics: The Rest of It3:27 with Ben Deitch
In this video we'll talk about some other important functions of Lists and see how to use them!
Being able to add or 0:00 remove items from a list is certainly a big improvement over arrays. 0:01 But in Java, lists can do a lot more than just add or remove. 0:06 Let's add a couple lines and then try out the index of function. 0:11 Let's create a new int variable named samIndex. 0:14 And set it equal to groceryline.indexOf, and pass in Sam. 0:18 Then, let's print out our samIndex. 0:25 And perfect, Sam is an index 1. 0:31 Now, let's see what happens when we look for an object that doesn't exist. 0:34 Instead of looking for Sam, let's look for Pam. 0:39 And if we run it, we get negative 1 because Pam is not in the list. 0:45 Another thing to keep in mind about indexOf 0:53 is that it starts its search from the beginning of the list. 0:55 So if our list had two Sams in it, indexOf would give us the index of the first Sam. 0:59 Another important function is the size function. 1:07 You can think of it as the length property of an array, except for a list. 1:10 And since lists can change sizes, it needs to be a function instead of a property. 1:15 Let's add a couple lines and try it out. 1:21 Let's sout, and then print out groceryLine.size. 1:23 Which gives us 2, nice. 1:30 One more thing you should know about lists is that you can use them 1:33 with the for each loop. 1:36 Let's add a couple lines, and then type for. 1:38 And inside, let's make a string variable called name. 1:42 Then add the colon, and the name of our list. 1:47 So for each string, which we'll call name, and 1:51 our grocery line list, we're going to do something. 1:55 And inside, let's just print out the name variable. 1:59 Great, that about does it for the really important functions of a list. 2:06 But I think it's worthwhile to quickly point out a couple more 2:11 in case you need them down the road. 2:14 Let's add a couple of more lines to the bottom of our main method, and 2:17 then type groceryLine., and just leave it at that. 2:20 We've already talked about most of these. 2:24 So I'll skip past the first few and 2:26 move on to the clear function, which will remove all the items from the list. 2:28 If we go a bit further down, we'll also find the set function, 2:33 which replaces the item at the specified index. 2:37 We've also go the two array functions, 2:40 which returns an array containing the same elements as our list. 2:43 So if you've got a function that takes in an array, but 2:47 you've only got a list, you're looking for the toArray method. 2:51 Also, if you're curious what any of the rest of these functions do, just select 2:55 it, and then once it's in the code, use Cmd or Ctrl+B to jump to its declaration. 2:59 Then, depending on who wrote the code, you might find some pretty good documentation. 3:07 Hopefully that gives you a pretty good overview of what lists are, and 3:15 how to use them. 3:19 As always if you have question head on to the community and ask away. 3:20 Until next time. 3:25
You need to sign up for Treehouse in order to download course files.Sign up