English subtitles

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

Get Embed Code
5 Languages

Showing Revision 4 created 05/25/2016 by Udacity Robot.

  1. Let's tackle this together. Here we have the rightmost value
  2. of the picture to the left. We'll add ten and then
  3. we translate pic by that amount. That's the statement. Now
  4. we need to do that for all of the pictures. So
  5. I need to add the loop header. I've done that
  6. often enough that my fingers can do it on autopilot. Here
  7. goes. Pretty soon your fingers will be able to do
  8. that too. Let's go and compile, and let's run the program.
  9. Hm, that did not seem to work, now did it? Let's see. The program crashed, and I
  10. am told that there is an ArrayIndexOutOfBoundsException, and the
  11. index that was out of bounds was minus one.
  12. Well, minus one is not a valid array index. And why would I have gotten a minus
  13. one. Oh look at this i is 0, so over here this i is 0 and then the
  14. i minus 1 is 0 minus 1 and that's bad. Now the problem is that when
  15. I am at the zeroth picture, that is the initial picture in this sequence. There
  16. is nothing to the left, so the remedy is to start here. With i equals 1. The
  17. zeroth picture just stays where it was. Picture at index one then gets moved to
  18. the right of the one at index zero, and
  19. that's all great. So in this case, my fingers shouldn't
  20. have been on auto pilot, but I should have thought
  21. through what values for i, I actually want. It's very,
  22. very common in actual programming to get these kinds of
  23. errors. And I wanted to show how it looks like
  24. when you do. Now let's try this. And look at
  25. that, our pictures are finally arranged in a neat sequence.