1 00:00:00,250 --> 00:00:01,480 Being able to add or 2 00:00:01,480 --> 00:00:06,000 remove items from a list is certainly a big improvement over arrays. 3 00:00:06,000 --> 00:00:11,060 But in Java, lists can do a lot more than just add or remove. 4 00:00:11,060 --> 00:00:14,960 Let's add a couple lines and then try out the index of function. 5 00:00:14,960 --> 00:00:18,900 Let's create a new int variable named samIndex. 6 00:00:18,900 --> 00:00:25,170 And set it equal to groceryline.indexOf, and pass in Sam. 7 00:00:25,170 --> 00:00:26,489 Then, let's print out our samIndex. 8 00:00:31,485 --> 00:00:34,141 And perfect, Sam is an index 1. 9 00:00:34,141 --> 00:00:39,190 Now, let's see what happens when we look for an object that doesn't exist. 10 00:00:39,190 --> 00:00:42,600 Instead of looking for Sam, let's look for Pam. 11 00:00:45,617 --> 00:00:53,080 And if we run it, we get negative 1 because Pam is not in the list. 12 00:00:53,080 --> 00:00:55,960 Another thing to keep in mind about indexOf 13 00:00:55,960 --> 00:00:59,430 is that it starts its search from the beginning of the list. 14 00:00:59,430 --> 00:01:05,260 So if our list had two Sams in it, indexOf would give us the index of the first Sam. 15 00:01:07,431 --> 00:01:10,970 Another important function is the size function. 16 00:01:10,970 --> 00:01:15,530 You can think of it as the length property of an array, except for a list. 17 00:01:15,530 --> 00:01:21,090 And since lists can change sizes, it needs to be a function instead of a property. 18 00:01:21,090 --> 00:01:23,520 Let's add a couple lines and try it out. 19 00:01:23,520 --> 00:01:28,543 Let's sout, and then print out groceryLine.size. 20 00:01:30,847 --> 00:01:33,440 Which gives us 2, nice. 21 00:01:33,440 --> 00:01:36,480 One more thing you should know about lists is that you can use them 22 00:01:36,480 --> 00:01:38,520 with the for each loop. 23 00:01:38,520 --> 00:01:42,800 Let's add a couple lines, and then type for. 24 00:01:42,800 --> 00:01:47,452 And inside, let's make a string variable called name. 25 00:01:47,452 --> 00:01:50,420 Then add the colon, and the name of our list. 26 00:01:51,660 --> 00:01:55,134 So for each string, which we'll call name, and 27 00:01:55,134 --> 00:01:59,041 our grocery line list, we're going to do something. 28 00:01:59,041 --> 00:02:01,760 And inside, let's just print out the name variable. 29 00:02:06,763 --> 00:02:11,430 Great, that about does it for the really important functions of a list. 30 00:02:11,430 --> 00:02:14,980 But I think it's worthwhile to quickly point out a couple more 31 00:02:14,980 --> 00:02:16,550 in case you need them down the road. 32 00:02:17,650 --> 00:02:20,840 Let's add a couple of more lines to the bottom of our main method, and 33 00:02:20,840 --> 00:02:24,650 then type groceryLine., and just leave it at that. 34 00:02:24,650 --> 00:02:26,590 We've already talked about most of these. 35 00:02:26,590 --> 00:02:28,690 So I'll skip past the first few and 36 00:02:28,690 --> 00:02:33,700 move on to the clear function, which will remove all the items from the list. 37 00:02:33,700 --> 00:02:37,570 If we go a bit further down, we'll also find the set function, 38 00:02:37,570 --> 00:02:40,870 which replaces the item at the specified index. 39 00:02:40,870 --> 00:02:43,440 We've also go the two array functions, 40 00:02:43,440 --> 00:02:47,920 which returns an array containing the same elements as our list. 41 00:02:47,920 --> 00:02:51,180 So if you've got a function that takes in an array, but 42 00:02:51,180 --> 00:02:55,190 you've only got a list, you're looking for the toArray method. 43 00:02:55,190 --> 00:02:59,950 Also, if you're curious what any of the rest of these functions do, just select 44 00:02:59,950 --> 00:03:07,050 it, and then once it's in the code, use Cmd or Ctrl+B to jump to its declaration. 45 00:03:07,050 --> 00:03:12,260 Then, depending on who wrote the code, you might find some pretty good documentation. 46 00:03:15,539 --> 00:03:19,325 Hopefully that gives you a pretty good overview of what lists are, and 47 00:03:19,325 --> 00:03:20,790 how to use them. 48 00:03:20,790 --> 00:03:25,440 As always if you have question head on to the community and ask away. 49 00:03:25,440 --> 00:03:26,210 Until next time.