Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,360 --> 00:00:00,690
All right.
1
2
00:00:00,710 --> 00:00:08,280
So in this lesson, we're going to do a deep dive on Dart lists. And we've already seen that we can create
2
3
00:00:08,280 --> 00:00:16,860
a list just as you would create a shopping list, by adding some items to the list and they each go in,
3
4
00:00:16,950 --> 00:00:17,730
in order.
4
5
00:00:18,120 --> 00:00:25,620
So for example in this case, if I was creating a guest list for my upcoming party, then as I add names
5
6
00:00:25,620 --> 00:00:28,540
to the list they will go in, in order.
6
7
00:00:28,890 --> 00:00:34,940
And remember that when we're dealing with computers, they always start counting from zero.
7
8
00:00:35,430 --> 00:00:43,110
So that means that the first item is actually going to be the zeroth item in the list, and then the order
8
9
00:00:43,110 --> 00:00:45,160
goes on logically.
9
10
00:00:45,180 --> 00:00:50,730
So when you're picking items from the list you would refer to them by their index.
10
11
00:00:50,730 --> 00:00:53,580
So in this case Angela is at index 0,
11
12
00:00:53,760 --> 00:01:00,960
and Katie is at index 2. And it's a little bit counterintuitive because of the fact that the list starts
12
13
00:01:01,050 --> 00:01:02,100
from 0.
13
14
00:01:02,880 --> 00:01:10,830
So if we were to pick the third from the list, it's actually not 1 2 3, but it's actually this
14
15
00:01:10,830 --> 00:01:15,970
one. We can see how this works using DartPad.
15
16
00:01:16,090 --> 00:01:21,040
Now DartPad actually allows you to share code that you've created.
16
17
00:01:21,040 --> 00:01:26,500
So I want to share with you a little bit of code that I made earlier on to make this exercise a little
17
18
00:01:26,500 --> 00:01:27,520
bit easier for you
18
19
00:01:27,580 --> 00:01:33,160
so you don't have to copy out all the names. And the way that you would share a DartPad is you would
19
20
00:01:33,160 --> 00:01:40,420
write some code, and create aGitHub gist. Now a GitHub gist, unlike your GitHub repos,
20
21
00:01:40,480 --> 00:01:47,380
these are just a single code file, and it allows you to share snippets of code around very very easily
21
22
00:01:47,730 --> 00:01:49,990
and it's completely free to create and to use.
22
23
00:01:50,830 --> 00:01:57,970
So I've created this just under my account. And now when I'm viewing this gist, I can see that it's under
23
24
00:01:57,970 --> 00:01:59,380
this address.
24
25
00:01:59,380 --> 00:02:07,540
So if I copy everything that comes after my username and a forward slash, then I can paste it at the
25
26
00:02:07,540 --> 00:02:11,710
end of the dartpad.dartlang.org website.
26
27
00:02:12,160 --> 00:02:15,700
So I'm going to add a forward slash and paste that in here.
27
28
00:02:15,700 --> 00:02:22,610
And now when I hit enter, it will load up that's just as a Dart file inside DartPad.
28
29
00:02:22,660 --> 00:02:29,550
So I will show you this URL, so that when you go to it, this is exactly what you will see.
29
30
00:02:29,590 --> 00:02:32,500
So we can work on the same file together.
30
31
00:02:33,040 --> 00:02:39,850
Once you're here, then you'll notice that I've already created a list of strings called myList.
31
32
00:02:39,850 --> 00:02:44,730
And inside the list, I've created it with some starting pieces of data.
32
33
00:02:44,740 --> 00:02:50,400
So I've got four names in here that matches exactly what you saw earlier in the slides.
33
34
00:02:50,410 --> 00:02:58,870
Now if I wanted to pick out the third item from my list, I would simply say my list, that's referring
34
35
00:02:58,870 --> 00:03:01,900
to this variable called myList,
35
36
00:03:01,900 --> 00:03:07,870
and then I would add a set of square brackets and inside I can provide an integer, so a whole number,
36
37
00:03:08,320 --> 00:03:12,960
and this can refer to the index of the item that I want to pick out of the list.
37
38
00:03:13,390 --> 00:03:21,850
So if I put the number 3 in here, then this is now going to be equal to the item inside my list
38
39
00:03:21,850 --> 00:03:24,280
that is at the third position.
39
40
00:03:24,310 --> 00:03:27,050
So zero, one, two, three.
40
41
00:03:27,070 --> 00:03:29,690
So this should now equal Jack.
41
42
00:03:29,890 --> 00:03:34,590
And we can confirm this by simply printing it into the console.
42
43
00:03:35,560 --> 00:03:39,390
So I'm going to wrap a print statement around it, and hit run.
43
44
00:03:39,580 --> 00:03:44,050
And you can see in here we're going to get Jack printed.
44
45
00:03:44,050 --> 00:03:45,150
So it's pulled out
45
46
00:03:45,150 --> 00:03:48,680
the third item out of the list.
46
47
00:03:48,890 --> 00:03:53,390
You can also try and get the index by providing the piece of data.
47
48
00:03:53,390 --> 00:04:00,230
So for example, if I was looking for Katie in my list, then I would get back that it's at index number
48
49
00:04:00,320 --> 00:04:12,840
2. And we can do this by simply saying myList.indexOf, and we have to provide the string that
49
50
00:04:12,840 --> 00:04:16,060
we're looking for which in this case is Katie.
50
51
00:04:16,160 --> 00:04:25,780
And it has to be spelt exactly the same way in terms of capitalization. And now when we print this instead,
51
52
00:04:25,800 --> 00:04:34,110
so let's comment that out and let's wrap a print statement around this that we've just created.
52
53
00:04:34,110 --> 00:04:44,370
Now when I hit run, we're going to get 2 in the console, because this is equal to 2. Now as we saw earlier,
53
54
00:04:44,370 --> 00:04:52,710
you can also add it to the list by simply using the .add, and then inside the parentheses you would
54
55
00:04:52,710 --> 00:04:55,280
provide whatever it is that you want to add.
55
56
00:04:55,380 --> 00:05:01,890
And remember that when you're adding to the list, the new item always goes at the end of the list not
56
57
00:05:01,920 --> 00:05:10,670
at the beginning or some random. So in this case, if we were to go ahead and comment this out, and I'm
57
58
00:05:10,670 --> 00:05:18,770
going to write myList.add and the thing that I'm going to add is a another person onto my guest
58
59
00:05:18,770 --> 00:05:19,500
list.
59
60
00:05:19,580 --> 00:05:28,580
And now if I print myList, then we can see what it now looks like all right.
60
61
00:05:28,640 --> 00:05:36,470
So let's run our code, and we can see that our list consists of Angela, James, Katie, Jack and Ben. And the
61
62
00:05:36,470 --> 00:05:43,490
console is very helpfully telling you that this is a list by showing you the data inside a square bracket.
62
63
00:05:45,160 --> 00:05:50,460
So we can see that whenever we use add, it always adds our data right to the end
63
64
00:05:50,460 --> 00:05:50,760
right?
64
65
00:05:51,010 --> 00:05:54,460
But if you wanted to add your data somewhere in between,
65
66
00:05:54,530 --> 00:06:03,140
there's also a way to do this. You can actually insert a piece of data into the list and push everything
66
67
00:06:03,140 --> 00:06:06,200
that came afterwards to the next index.
67
68
00:06:06,200 --> 00:06:13,700
So in this case, Ben came in here and it was inserted into position 2 which displaced the previous item
68
69
00:06:13,700 --> 00:06:21,160
that was at position 2 to position 3. And the way that we would do this is we would write instead
69
70
00:06:21,160 --> 00:06:30,940
of myList.add, we would instead say myList.insert. And we have to provide a index,
70
71
00:06:30,940 --> 00:06:37,330
so where do we want to insert it in the in the index of our list? And also what is it that we want to
71
72
00:06:37,330 --> 00:06:37,940
insert?
72
73
00:06:38,290 --> 00:06:45,250
So Ben is going to be promoted to position 2 and of course the string that we want to insert, is the
73
74
00:06:45,250 --> 00:06:46,410
name Ben.
74
75
00:06:46,480 --> 00:06:51,040
So now if we had run, you can see that previously Ben got added at the end,
75
76
00:06:51,040 --> 00:06:58,960
now Ben is going to show up at position 2. Remember our list starts from 0, 1, 2.
76
77
00:06:59,050 --> 00:07:04,990
Now there's a whole lot of other things that you can do with lists and you can discover some of those
77
78
00:07:05,230 --> 00:07:06,460
even as you're typing.
78
79
00:07:06,460 --> 00:07:12,990
So when you write myList. you can see that DartPad is already suggesting all of these things
79
80
00:07:13,000 --> 00:07:14,660
I can do using lists.
80
81
00:07:14,770 --> 00:07:21,890
For example, I can try and get the first item in my list by simply writing myList.first.
81
82
00:07:22,090 --> 00:07:27,410
And I could also get the last item in the list by writing .last.
82
83
00:07:27,520 --> 00:07:34,240
And there's also loads of other things I can do like sort my list, or remove things in my list.
83
84
00:07:34,240 --> 00:07:40,240
And if you want to learn more about all of these things that you can do with Dart lists, I'll add a link
84
85
00:07:40,300 --> 00:07:41,280
to this
85
86
00:07:41,290 --> 00:07:48,310
URL, which is a page on the Dart language guide where it talks about lists and all of the things
86
87
00:07:48,310 --> 00:07:52,030
that you can do using Dart lists in more detail.
87
88
00:07:52,030 --> 00:07:58,030
So I recommend browsing through this website and seeing, at a glance, what are the things that you can
88
89
00:07:58,030 --> 00:07:59,090
do with lists.
89
90
00:07:59,200 --> 00:08:05,350
But there's no need to memorize them because once you come across a actual use case, when you find the
90
91
00:08:05,350 --> 00:08:10,300
need to use a particular functionality of the list, then you can simply search for it and you'll come
91
92
00:08:10,300 --> 00:08:15,340
across either a piece of documentation like this or a post on Stack Overflow where you'll be able to
92
93
00:08:15,340 --> 00:08:20,190
use it as you need to. That's all on list. In the next lesson,
93
94
00:08:20,210 --> 00:08:24,500
we're going to get back to coding up our app and we're going to start displaying some questions.
10603
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.