1 00:00:00,660 --> 00:00:04,000 In this video, we learn about lists and list buffers. 2 00:00:04,000 --> 00:00:08,390 A list is another data structure which maintains the order of the elements, 3 00:00:08,390 --> 00:00:11,370 allow for duplication and is immutable. 4 00:00:11,370 --> 00:00:16,480 Similarly to the array buffer, the list buffer is the mutable form of a list, 5 00:00:16,480 --> 00:00:18,569 let's dive in and implement some lists. 6 00:00:19,950 --> 00:00:22,860 There are a few ways to create a list in Scala. 7 00:00:22,860 --> 00:00:26,790 However, the simplest form is by calling the list keyword. 8 00:00:26,790 --> 00:00:30,290 Let's jump back into the code and implement some lists. 9 00:00:30,290 --> 00:00:34,585 Back into our collections package in our super hero object, we'll clean up 10 00:00:34,585 --> 00:00:38,897 everything associated to arrays and array buffers, just for simplicity. 11 00:00:56,099 --> 00:00:59,900 Awesome, this creates an immutable list of type string. 12 00:00:59,900 --> 00:01:06,052 We can additionally create a list in Scala by using the cons operator noted by colon, 13 00:01:06,052 --> 00:01:09,879 colon and the keyword nil at the tail end of the list. 14 00:01:18,007 --> 00:01:21,631 By having cons with nil at the end of the statement, 15 00:01:21,631 --> 00:01:25,272 creates a list that holds the numbers 1,2,3. 16 00:01:25,272 --> 00:01:30,616 Therefore, numbers is a list of integers containing one, two and three. 17 00:01:30,616 --> 00:01:36,060 Lists are very fast at retrieving the first and the last element in the list. 18 00:01:36,060 --> 00:01:38,970 To get the first element, we call the head method. 19 00:01:38,970 --> 00:01:43,260 And for any element besides the first, we can call the tell method. 20 00:01:43,260 --> 00:01:47,056 This is probably different from what you may expect in other languages. 21 00:01:53,440 --> 00:02:00,456 Let's compile and rerun our app. 22 00:02:00,456 --> 00:02:05,823 As expected the immutable heroes that head returns the first element in our list, 23 00:02:05,823 --> 00:02:06,847 Wonder Woman. 24 00:02:06,847 --> 00:02:10,928 Additionally, the immutable heroes that tale returns everything but 25 00:02:10,928 --> 00:02:12,580 the first element. 26 00:02:12,580 --> 00:02:17,180 Therefore, a list including Thor, Superman, and Batman. 27 00:02:17,180 --> 00:02:20,610 Another neat function which applies to many collections 28 00:02:20,610 --> 00:02:23,330 as well as lists is MK string. 29 00:02:23,330 --> 00:02:28,451 MK string or Make String can be used to print the elements of a collection as well 30 00:02:28,451 --> 00:02:33,356 as it allows you to add a prefix or a suffix to any element when it is printed. 31 00:02:54,181 --> 00:02:59,550 In this example, we provide a prefix and our suffix to our immutable heroes list. 32 00:02:59,550 --> 00:03:02,830 For each element, we use a comma separator. 33 00:03:02,830 --> 00:03:05,479 Let's recompile our app and print out our results. 34 00:03:12,086 --> 00:03:16,145 Awesome, similar to how arrays have a mutable array buffer, 35 00:03:16,145 --> 00:03:19,430 lists also have a mutable list buffer. 36 00:03:19,430 --> 00:03:23,090 To create a list buffer, the mutable form of a list, 37 00:03:23,090 --> 00:03:27,010 we need to import it from a Scala collections package. 38 00:03:27,010 --> 00:03:29,114 Letâ€™s do so at the top of our file. 39 00:03:36,701 --> 00:03:41,267 Importing the package provides us with the flexibility to add, remove or 40 00:03:41,267 --> 00:03:43,779 even update our superheroes over time. 41 00:04:01,110 --> 00:04:04,897 We can now append to the list buffer by using the append method or 42 00:04:04,897 --> 00:04:07,890 simply providing the symbol plus equals. 43 00:04:07,890 --> 00:04:12,160 In order to prepend, we would use the plus equals colon symbol. 44 00:04:12,160 --> 00:04:13,210 Let's check these out. 45 00:04:31,377 --> 00:04:33,530 Let's recompile and run our app. 46 00:04:36,930 --> 00:04:40,690 Awesome, we prepended Captain America to our list, and 47 00:04:40,690 --> 00:04:44,270 we also append Raven to the end of the list. 48 00:04:44,270 --> 00:04:48,850 In order to access an element of the list, we use the parentheses notation. 49 00:04:48,850 --> 00:04:51,076 And we provide the index based value. 50 00:04:58,929 --> 00:05:03,740 Because list buffers are mutable, we can also update a specific element. 51 00:05:03,740 --> 00:05:06,325 Let's remove Thor and replace him with Iron Man. 52 00:05:12,742 --> 00:05:16,764 Let's comment out on our print line statements and check out our results. 53 00:05:31,571 --> 00:05:35,707 We can remove elements from a list, using the minus equals symbol, or 54 00:05:35,707 --> 00:05:37,930 calling the remove method. 55 00:05:37,930 --> 00:05:41,570 Additionally, we can remove elements by value or even by index. 56 00:05:42,620 --> 00:05:45,103 Let's start by removing an element by value. 57 00:05:51,846 --> 00:05:55,427 The tarsal remove an element in mutable heroes by index. 58 00:06:07,935 --> 00:06:08,610 Awesome. 59 00:06:08,610 --> 00:06:13,580 We get back Wonder Woman, Iron Man, Superman, and Raven which means 60 00:06:13,580 --> 00:06:18,770 that we're able to successfully remove Batman and also Captain America. 61 00:06:18,770 --> 00:06:21,910 Great, we see how list and list buffers work. 62 00:06:21,910 --> 00:06:25,060 In the next video, we will dive into maps and tuples and 63 00:06:25,060 --> 00:06:27,380 see how they're useful to our everyday code.