Problem connecting to Twitter. Please try again.
Problem connecting to Twitter. Please try again.
Problem connecting to Twitter. Please try again.
Problem connecting to Twitter. Please try again.
Problem connecting to Twitter. Please try again.
Intro to Arrays (Video Version)
-
0:01 - 0:04You already learned about using variables
to store numbers or strings. -
0:04 - 0:06Now we're going to learn
about something called arrays, -
0:06 - 0:10which let us store multiple items
in just one variable. -
0:10 - 0:13As you'll see, arrays let us do
all kinds of useful things. -
0:13 - 0:15Okay, let's review variables. All right?
-
0:15 - 0:19So we have, var myFriend = "Sophia".
-
0:20 - 0:23So, a variable is just a way
of storing a value, like Sofia, -
0:23 - 0:27and giving it a label so our program
has an easy way to refer to it later. -
0:27 - 0:28We can think of it like a drawer,
-
0:28 - 0:32with a myFriend label on the outside
and Sofia on the inside. -
0:32 - 0:35So whenever we look inside it,
we find Sofia. -
0:36 - 0:40Now, sometimes we want to hold
multiple values in a single variable -
0:40 - 0:44and we can't just do it like this
because we'll get a syntax error -
0:44 - 0:46and our program will freak out,
and all that stuff. -
0:46 - 0:49So we have a specific way
that we do that, -
0:49 - 0:51and that's using arrays.
-
0:51 - 0:53So we could say, myFriends = ,
-
0:53 - 0:56and then we have to do
a square bracket, -
0:56 - 0:59and then inside,
that's where we can put all the values. -
0:59 - 1:04So we say, Sofia,
and we have John, and we have Leif. -
1:04 - 1:06All right, so those are my three friends.
-
1:06 - 1:09Maybe in order of how much I like them,
but don't tell them that. -
1:09 - 1:13Okay, so now our variable
holds three values, not just one. -
1:13 - 1:15And we can imagine
it's like a chest of drawers, -
1:15 - 1:18and we put this label, myFriends,
on the whole chest. -
1:18 - 1:20And we can just open the right drawer
-
1:20 - 1:23to find the value
we're looking for, right? -
1:24 - 1:25And you imagine, with a chest of drawers,
-
1:25 - 1:28if you want to know
what's inside the first drawer, -
1:28 - 1:30you would just open it up and look inside.
-
1:30 - 1:32So how do we do that with our array?
-
1:32 - 1:34Well, we can just type
the name of the array, -
1:34 - 1:36and then the brackets again--
-
1:36 - 1:38ooh, I misspelled it--
-
1:39 - 1:43and then the number
of whatever it is in the array, right? -
1:43 - 1:45So maybe it would be 1. Okay?
-
1:45 - 1:47So, let's actually try this out
-
1:47 - 1:51by using the text command
and showing Sofia on the canvas. -
1:51 - 1:55So we say, myFriends--
ooh, friend is a hard word to spell, huh? -
1:55 - 2:00So, myFriends[1],
and then we put it here. -
2:00 - 2:02Oh, and then let's put a little fill.
-
2:02 - 2:06Oh, okay, so we see John.
Why do we see John? -
2:06 - 2:09We said 1 for the element index, right?
-
2:09 - 2:11The 1 right here.
-
2:11 - 2:16Well, that's because
arrays start at 0, not 1, -
2:16 - 2:20and it'll definitely seem weird at first,
but you'll get used to it. -
2:20 - 2:24So if we put 0,
then we see Sofia, all right? -
2:24 - 2:26And then, if we want to show
the next element, -
2:26 - 2:28then we use 1, all right?
-
2:28 - 2:30So, and then if we want to do
the final element, -
2:30 - 2:32the third element, then we use 2.
-
2:33 - 2:37So you just think to yourself:
"Okay, which one do I want to retrieve?"-- -
2:37 - 2:38Oh, let's spread these out--
-
2:38 - 2:41and where is it located,
and you just subtract one. -
2:41 - 2:44So the first one is 0,
the second one is 1, -
2:44 - 2:47the third one is 2, et cetera, et cetera.
-
2:47 - 2:52What happens if I forgot,
and I try to access Leif this way? -
2:52 - 2:56Well then, we say myFriends[3],
and we'll just get nothing. -
2:56 - 2:58That's because there's nothing there, right?
-
2:58 - 3:02When it says 3,
it looks for the fourth element, -
3:02 - 3:05and there's no fourth element,
so there's just nothing. -
3:05 - 3:08And that's something that can happen a lot
when you're using arrays, -
3:08 - 3:10so just look out for that.
-
3:10 - 3:12And the same thing
if I tried to access a hundred -
3:12 - 3:15because I don't have a hundred friends,
I only have three, -
3:15 - 3:19so then we get nothing, all right?
So let's get rid of those. -
3:19 - 3:22Now, let's say we want to keep track
of how many friends we have -
3:22 - 3:24because I'm really proud,
and I have three friends, -
3:24 - 3:26and I want to let everybody know.
-
3:26 - 3:29So I'm going to go
and declare this to the world. -
3:29 - 3:33So, "I have " + numFriends + " friends!!!".
-
3:33 - 3:34Woo, all right.
-
3:37 - 3:40Okay, so I have three friends. Yay!
Oh, that's not very many. -
3:40 - 3:44Okay, so maybe Winston feels bad for me
and says he'll be my friend, -
3:44 - 3:46and he says I can add him to the array.
-
3:46 - 3:47And I was like:
"Okay, cool. Thanks, Winston." -
3:47 - 3:49So I add Winston.
-
3:49 - 3:51Oh, but it still says
I have three friends, right, -
3:51 - 3:54because I have to go
and update this variable here. -
3:54 - 3:57That means, every time
that I add something to this array, -
3:57 - 4:00I have to update this variable,
and that could get really annoying, -
4:00 - 4:01especially if all of you guys
watching this -
4:01 - 4:03decide you'll be my friend.
-
4:03 - 4:05And then, you know,
I'm updating this thousands of times -
4:05 - 4:08and having to update this every time.
-
4:08 - 4:09So here's the thing:
-
4:09 - 4:14We, so often, want to know
how long our array is, -
4:14 - 4:16that there's a special way to do that.
-
4:16 - 4:18So the array will keep track
of how long it is -
4:18 - 4:20using a property called length.
-
4:20 - 4:25And to use it, we just say,
myFriends.length, -
4:25 - 4:27and then we'll get back the length.
-
4:27 - 4:29See, now it says 4,
and I can delete this variable. -
4:29 - 4:31I don't need it any more.
-
4:31 - 4:33And this property will update
whenever we add. -
4:33 - 4:35So maybe OhNoes Guy!!
says he'll be my friend, and I'm like: -
4:35 - 4:39"Okay, you're kind of mean,
but okay, you'll be my friend." -
4:40 - 4:43And we can keep adding
and it'll keep updating. -
4:43 - 4:47So, that's really cool because, you know,
-
4:47 - 4:50it's a lot less work to keep track
of how long our array is. -
4:50 - 4:51All right, so pretty much,
-
4:51 - 4:54whenever you want to store
a list of values like this, -
4:54 - 4:55we'll use an array.
-
4:56 - 4:57So keep watching to find out
all the really cool things -
4:57 - 4:59that we can use them for.
Terri Adams approved English subtitles for Intro to Arrays (Video Version) | ||
Terri Adams edited English subtitles for Intro to Arrays (Video Version) | ||
Terri Adams edited English subtitles for Intro to Arrays (Video Version) | ||
Robbie Jaeger edited English subtitles for Intro to Arrays (Video Version) |