Return to Video

Look at the Element Before this One - Intro to Java Programming

  • 0:00 - 0:04
    Let's tackle this together. Here we have the rightmost value
  • 0:04 - 0:07
    of the picture to the left. We'll add ten and then
  • 0:07 - 0:10
    we translate pic by that amount. That's the statement. Now
  • 0:10 - 0:13
    we need to do that for all of the pictures. So
  • 0:13 - 0:16
    I need to add the loop header. I've done that
  • 0:16 - 0:19
    often enough that my fingers can do it on autopilot. Here
  • 0:19 - 0:21
    goes. Pretty soon your fingers will be able to do
  • 0:21 - 0:25
    that too. Let's go and compile, and let's run the program.
  • 0:25 - 0:31
    Hm, that did not seem to work, now did it? Let's see. The program crashed, and I
  • 0:31 - 0:37
    am told that there is an ArrayIndexOutOfBoundsException, and the
  • 0:37 - 0:40
    index that was out of bounds was minus one.
  • 0:40 - 0:45
    Well, minus one is not a valid array index. And why would I have gotten a minus
  • 0:45 - 0:51
    one. Oh look at this i is 0, so over here this i is 0 and then the
  • 0:51 - 0:58
    i minus 1 is 0 minus 1 and that's bad. Now the problem is that when
  • 0:58 - 1:03
    I am at the zeroth picture, that is the initial picture in this sequence. There
  • 1:03 - 1:11
    is nothing to the left, so the remedy is to start here. With i equals 1. The
  • 1:11 - 1:16
    zeroth picture just stays where it was. Picture at index one then gets moved to
  • 1:16 - 1:18
    the right of the one at index zero, and
  • 1:18 - 1:22
    that's all great. So in this case, my fingers shouldn't
  • 1:22 - 1:24
    have been on auto pilot, but I should have thought
  • 1:24 - 1:28
    through what values for i, I actually want. It's very,
  • 1:28 - 1:32
    very common in actual programming to get these kinds of
  • 1:32 - 1:34
    errors. And I wanted to show how it looks like
  • 1:34 - 1:37
    when you do. Now let's try this. And look at
  • 1:37 - 1:41
    that, our pictures are finally arranged in a neat sequence.
タイトル:
Look at the Element Before this One - Intro to Java Programming
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
cs046: Intro to Programming
Duration:
01:42

English subtitles

改訂 Compare revisions