English subtitles

← Enhanced For Loop - Intro to Java Programming

Get Embed Code
5 Languages

Showing Revision 5 created 07/06/2017 by Dejan Trajkovic.

  1. This problem is a little bit tricky, because you have to completely rethink the
  2. approach. Previously, we looked at a picture,and the one to the left. But when
  3. we use an enhanced for loop, we only get to look at one picture at a time, so we
  4. have to remember something from the past. The way I've done it is, I would
  5. remember the rightmost position of the previous picture. Let me show you. I
  6. have an enhanced for loop, and when I'm done positioning the picture, I
  7. remember its rightmostX value. So when I reenter the loop with the next
  8. picture, then I can position it 10 further than the rightmostX value. And
  9. they're always spaced apart nicely. What about he first picture? If I start the
  10. rightmostX at 0, then the first picture ends up at offset 10. So that's the
  11. answer. And now when you look at these two loops, you can see that we can
  12. actually merge them. So we do the positioning and drawing in a single loop.
  13. That's enough about pictures for right now. Sarah will show you another
  14. application of array lists. She'll revisit a class that you've seen in lesson
  15. three. The person class that collected friends, at that time we did it in
  16. a less than perfect way by just having one long string of friend names. Now
  17. that you know how to collect objects, she'll collect the friends in an array
  18. list. Alright, over to you Sarah.