< Return to Video

9.3: Arrays of Objects - Processing Tutorial

  • 0:00 - 0:02
    Okay we're getting close now, we're
    getting close.
  • 0:02 - 0:05
    There's just this moment, I can see it,
    there's two bubbles, we're soon
  • 0:05 - 0:08
    going to have a thousand bubbles,
    it's very exciting.
  • 0:08 - 0:11
    So we're, what do we have so far?
    We know how to declare,
  • 0:11 - 0:13
    this list of numbers; integers.
  • 0:13 - 0:16
    We were going to look at an example that
    used a list of integers,
  • 0:16 - 0:19
    and maybe that's a bit of a mistake,
    and I'll try to fit that in again
  • 0:19 - 0:21
    somewhere. But you know, think about that,
    how might you do that?
  • 0:21 - 0:26
    But what I want to move straight to is
    saying, "Let's apply this thinking
  • 0:26 - 0:30
    to these bubbles." And instead of having
    separate bubble object variables, lets
  • 0:30 - 0:33
    have a single list of bubble objects.
  • 0:33 - 0:37
    So if we look at this syntax, the first
    thing we have is the type of the array.
  • 0:37 - 0:40
    Now we're going to have a type "Bubble"
    the object "Bubble".
  • 0:40 - 0:44
    We need to make sure it's going to be an
    array, great we need to give our array a
  • 0:44 - 0:49
    name, let's call it "Bubbles".
    The wire's a good joke, just thinking of
  • 0:49 - 0:53
    the wire, because of the name, "Bubbles",
    but you didn't really need to know that.
  • 0:55 - 0:58
    I think I should make some magic way,
    where I could just like, edit out
  • 0:58 - 0:59
    unnecessary thoughts.
  • 0:59 - 1:04
    Okay um... and then we need to say how
    many bubbles are we going to have in our
  • 1:04 - 1:09
    array; we're going to have two. We're not
    getting very ambitious here, we're getting
  • 1:09 - 1:14
    very very basic simple, we're going to
    have just two, um, I also by the way
  • 1:14 - 1:17
    noticed, there's this little monitor that
    I look at, which is slightly above the
  • 1:17 - 1:19
    camera, I feel like I'm always looking
    above the camera. But if I look this way,
  • 1:19 - 1:22
    I'm looking right at the camera, right at
    you.
  • 1:22 - 1:29
    Okay, so let's try to apply this here.
    So, what's going on? Look, bubble, let's
  • 1:29 - 1:32
    close this, Bubble b1; Bubble b2;
    first of all, you know
  • 1:32 - 1:38
    we really should've done this, just
    to get us in that frame of reference, of
  • 1:38 - 1:41
    thinking of counting from zero,
    from the rest of your life, you should
  • 1:41 - 1:44
    just count from zero from now on,
    always start at zero.
  • 1:44 - 1:50
    Okay, uh... what I want to do is instead
    of having two separate variables, I want
  • 1:50 - 1:58
    to have an array, called "bubbles",
    it's going to be of type "Bubble", it's
  • 1:58 - 2:00
    got the brackets, which means it's an
    array, we name this "bubbles",
  • 2:00 - 2:05
    and it has two bubbles in it. There's a
    lot of bubble everywhere but you know,
  • 2:05 - 2:07
    this is what's happening to us right now.
  • 2:07 - 2:20
    So we can get rid of this. Now, instead of
    initializing a single bubble...
  • 2:20 - 2:26
    we can now refer to the bubbles by their
    index value, we can say, "Bubbles index 0
  • 2:26 - 2:30
    make a new bubble." "Bubbles index 1 make
    a new bubble."
  • 2:30 - 2:35
    So, its like we had two separate variables
    but both of those variables exist as spots
  • 2:35 - 2:39
    in a list. You can imagine this list with
    two spots in it, and there's one bubble
  • 2:39 - 2:45
    here, and another bubble there.
    Um, okay, so what do we do now?
  • 2:45 - 2:48
    We need to take this idea and say,
  • 2:48 - 2:52
    bubbles[0].ascend();
    bubbles[0].display();
    bubbles[0].top();
  • 2:52 - 3:00
    bubbles[1].ascend();
    bubbles[1].display();
    bubbles[1].top();
  • 3:03 - 3:08
    There we go, we have the identical
    program, just what we had before.
  • 3:08 - 3:14
    Okay, so you know what? I'm just going
    with this, I'm experimenting with shorter
  • 3:14 - 3:16
    videos, this has only been two, two and a
    half minutes (3 mins 17 seconds actually),
  • 3:16 - 3:21
    but let's actually stop here, so this is a
    good, obviously this is a huge problem
  • 3:21 - 3:25
    right? We've kind of done nothing to, to
    get towards our goal of having a thousand
  • 3:25 - 3:30
    bubbles right? Yes, we've put it in a list
    but we still have individual lines of code
  • 3:30 - 3:33
    for each bubble, we have to say,
    bubbles[0].all the functions;
  • 3:33 - 3:34
    bubbles[1].all the functions;
  • 3:34 - 3:37
    then if we had a thousand, two and three
    and four all the way up to 999.
  • 3:37 - 3:41
    So, we're on the way there but we're
    missing an important step, and that
  • 3:41 - 3:44
    important step is going to be using a loop,
    and that's we're going to be doing in the
  • 3:44 - 3:47
    next video. But, let's take a moment to
    pause here and what I would say to you as
  • 3:47 - 3:51
    an exercise is go find something where you
    had an object and try to make an array of
  • 3:51 - 3:56
    those objects. Try to make multiple
    objects and use the array, don't just
  • 3:56 - 3:59
    and refer to which object by its index
    value.
  • 3:59 - 4:04
    You can do interesting things like, um, I
    could say, if I wanted to that this second
  • 4:04 - 4:08
    one shouldn't bother to ascend, only the
    first one [needs to ascend].
  • 4:08 - 4:11
    You can see the second one, now doesn't
    have that function called on it, so it's
  • 4:11 - 4:17
    just laying there at the bottom, so that's
    what I would say to you, take your object
  • 4:17 - 4:21
    that you made make an array of them and
    try to see how that works.
Title:
9.3: Arrays of Objects - Processing Tutorial
Description:

more » « less
Video Language:
English
Duration:
04:22

English subtitles

Revisions