Return to Video

Intro to Arrays (Video Version)

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

This is an introduction to arrays and how to use them to store multiple numbers or strings. This also introduces how to get information about arrays, such as the length of the array.

more » « less
Video Language:
English
Duration:
05:00

English subtitles

Revisions