< Return to Video

Modifying Arrays (Video Version)

  • 0:01 - 0:05
    So far, we've seen how to create
    an array and access it.
  • 0:05 - 0:09
    Just like other variables, however,
    one of the really cool things about arrays
  • 0:09 - 0:13
    is the way we can change them dynamically
    while a program is running.
  • 0:13 - 0:16
    OK, let me just show you
    what I mean by this.
  • 0:16 - 0:18
    So here we have this program
  • 0:18 - 0:22
    that displays Hopper
    holding some balloons; super cute.
  • 0:22 - 0:25
    And so how it works is that we have
    this xPositions array
  • 0:25 - 0:30
    that has two numbers in it that say
    where we want the balloons at.
  • 0:30 - 0:32
    And then down here we have this loop,
  • 0:32 - 0:36
    and this loop goes through
    every element in that array.
  • 0:36 - 0:40
    And for each of them, it draws a line
    from the "x" down to Hopper's hand,
  • 0:40 - 0:46
    and then draws an ellipse at the "x"
    that's 30x40 pixels,
  • 0:46 - 0:48
    and that's our balloon.
  • 0:48 - 0:51
    Okay, cool.
    So now that we see how it works,
  • 0:51 - 0:54
    we know that if we want
    to have another balloon show up,
  • 0:54 - 0:59
    we can just add a number
    to this array, right? Like 300.
  • 0:59 - 1:03
    Beautiful. Now we've got three balloons
    for a happy Hopper.
  • 1:03 - 1:09
    But let's say we wanted to give a user
    that doesn't know how to code
  • 1:09 - 1:11
    the ability to add new balloons.
  • 1:11 - 1:13
    So we wanted to give the program to a user
  • 1:13 - 1:17
    and say, "Hey, you can click
    wherever you want the balloon,
  • 1:17 - 1:21
    and it will show up."
    Wouldn't that be cool? I think so.
  • 1:21 - 1:24
    So, how should we do this?
  • 1:24 - 1:26
    So we want our program
    to be changing over time, right?
  • 1:26 - 1:31
    Every time the user clicks,
    a balloon is going to show up there.
  • 1:31 - 1:35
    So let's start off by moving everything
    into a draw function
  • 1:35 - 1:37
    so that it's easy to change over time.
  • 1:37 - 1:43
    So we'll just move this down here
    and indent this here. OK, great.
  • 1:43 - 1:49
    So now we want to check and see
    is the user pressing the mouse right now?
  • 1:49 - 1:53
    Well we can do that with our "if".
    So if mouse if pressed,
  • 1:54 - 1:57
    and then we're going to do something.
    So what are we going to do?
  • 1:57 - 2:02
    If the mouse is pressed, then we want
    to somehow add a number to this array.
  • 2:02 - 2:05
    And let's make this
    just two elements again. Ok.
  • 2:05 - 2:08
    So we want to add a number
    to this array somehow.
  • 2:08 - 2:11
    Well I'll show you one way
    we could do this.
  • 2:11 - 2:18
    So we can say xPositions[2] = mouseX;
  • 2:19 - 2:22
    Alright, and let me just
    show you that this works.
  • 2:22 - 2:27
    I clicked, and ta-da! I got a balloon!
    So what did this do?
  • 2:27 - 2:32
    This said, xPositions[2] said
    find this array,
  • 2:32 - 2:35
    and find the element in the 2 spot,
  • 2:35 - 2:38
    and remember that's the third element
    because our arrays are zero-based.
  • 2:38 - 2:41
    And if you look,
    there's no third element, right?
  • 2:41 - 2:42
    There's nothing in that spot.
  • 2:42 - 2:46
    So it says find that,
    and then put mouseX in it.
  • 2:46 - 2:50
    Well since there was nothing there,
    then it goes from being nothing
  • 2:50 - 2:51
    to being mouseX.
  • 2:51 - 2:55
    And so now our array is three items long,
  • 2:55 - 2:57
    and this for loop down here
    that goes through it,
  • 2:57 - 3:00
    it will end up drawing that third balloon.
  • 3:00 - 3:03
    So that's pretty cool,
    and let me just click some more
  • 3:03 - 3:07
    to show you how this works.
    So you see every time I click,
  • 3:07 - 3:11
    it keeps on drawing the third balloon
    wherever I clicked my mouse.
  • 3:11 - 3:16
    And that's because we're constantly
    overriding the 2 spot,
  • 3:16 - 3:19
    the thing with the 2 index.
  • 3:19 - 3:23
    We're constantly overriding that
    with the current mouseX.
  • 3:23 - 3:26
    So we're only ever
    going to have three balloons
  • 3:26 - 3:30
    because we've got this one in the 0 spot,
    this one in the 1 spot,
  • 3:30 - 3:35
    and we're constantly changing
    the 2 spot. Ok?
  • 3:35 - 3:37
    So that's cool, but what we really want
  • 3:37 - 3:40
    is we want to let the user make
    tons of balloons, right?
  • 3:40 - 3:44
    So every time the user clicks,
    there's a new balloon.
  • 3:44 - 3:46
    So that means that we need
    to be constantly incrementing
  • 3:46 - 3:51
    the index of the array element
    that we're storing it in.
  • 3:51 - 3:54
    So we don't want it to be 2 every time,
    we want it to be 2, and then 3,
  • 3:54 - 3:56
    and then 4, and then 5, and then 6, etc.
  • 3:56 - 3:59
    So we could do this by having
    a little counter variable.
  • 3:59 - 4:01
    So we say newInd = 2;
  • 4:01 - 4:03
    That's what it will start out with,
  • 4:03 - 4:06
    and then here we'll say
    newInd instead of 2.
  • 4:06 - 4:10
    And then what we really want to do
    is say newInd ++
  • 4:10 - 4:12
    so every time we add 1 to this.
  • 4:12 - 4:15
    So it'll start off as 2,
    then become 3, and then become 4.
  • 4:15 - 4:18
    So every time they press,
    it will become more. So let's try this.
  • 4:18 - 4:22
    Ta-da! Tons of balloons.
    Balloon party. Woo!
  • 4:22 - 4:25
    So that's cool, right?
  • 4:25 - 4:27
    But that's not the best way of doing this
  • 4:27 - 4:30
    because it turns out
    adding items to an array
  • 4:30 - 4:32
    is something we want to do a lot.
  • 4:32 - 4:35
    So we have a much easier way
    of doing it than this.
  • 4:35 - 4:39
    So let me just delete the stuff we did.
    Alright, so we don't need that,
  • 4:39 - 4:43
    and we don't need that anymore.
    We'll just comment that out.
  • 4:43 - 4:48
    Ok, so how we do it is
    we say xPositions.push
  • 4:48 - 4:51
    and then mouseX.
  • 4:51 - 4:55
    So what we're doing here is
    we're calling this method
  • 4:55 - 4:56
    on the xPositions array.
  • 4:56 - 4:58
    So we're calling a command on the array.
  • 4:58 - 5:02
    We're telling the array,
    "Hey, push this new value,
  • 5:02 - 5:05
    which is mouseX,
    push it on the the end of your array."
  • 5:05 - 5:09
    So every time this gets called,
    so every time they press the mouse,
  • 5:09 - 5:13
    it's going to look at the mouseX
    and push it onto the end of the array.
  • 5:13 - 5:15
    So that the array should get bigger
    and bigger and bigger.
  • 5:15 - 5:17
    So let's restart and try this.
  • 5:18 - 5:19
    Ta-da, it worked!
  • 5:19 - 5:23
    And it's way less code
    than what we had before. Alright?
  • 5:23 - 5:25
    So most of the time,
    you're going to want to use push
  • 5:25 - 5:27
    if you're going to add stuff
    to your array like this.
  • 5:27 - 5:30
    And it's pretty neat because
    then you can just have these arrays
  • 5:30 - 5:32
    that get bigger and bigger
    and bigger during the program.
  • 5:32 - 5:35
    Like when you have an animation
    or when you have users doing stuff,
  • 5:35 - 5:36
    and then you can do a lot more.
  • 5:36 - 5:40
    So now you seen 90% of what
    you'll probably use arrays for
  • 5:40 - 5:42
    and the ways you'll use them.
  • 5:42 - 5:44
    But there's also a lot more
    that you can do with arrays.
  • 5:44 - 5:47
    So if you have questions,
    just ask them in the discussion.
  • 5:47 - 5:50
    But make sure
    that you master these basics first.
Title:
Modifying Arrays (Video Version)
Description:

This video shows how to dynamically change the contents of an array while the program is running.

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

English subtitles

Revisions