jga & the java 1.5 forloop

David Hall
2004-05-16
2004-09-18
  • David Hall

    David Hall - 2004-05-16

    One of the new features in Java 1.5 is the new forloop syntax. The new
    forloop is analagous to a foreach construct that is common in other
    languages. The syntax hides the existance of an iterator, and is even
    extended to cases that didn't have an iterator before (arrays). It
    also extends to enumerated types.

    So, if hiding the iterator is a step forward, can we take things
    another step and capture more of the common patterns as part of the
    loop mechanism? The answer is 'sort of'. We can't completely hide the
    fact that decisions are being made, and that some elements are being
    processed. What we can do is hide a lot of the mechanics of the
    process.

    (full article at http://jga.sf.net/docs/AddingAlgorithms.shtml\)

     
    • Michael Lorton

      Michael Lorton - 2004-09-17

      I notice a number of lines like the following:

      List<Fruit> fruits = Arrays.asList(new Fruit[]
          {apple, banana, blackberry, blueberry });

      The array declaration is superfluous; the following will work just as well and is (IMO) easier to read:

      List<Fruit> fruits = Arrays.asList(
              apple, banana, blackberry, blueberry );

      This is actually a varargs -- another benefit from Java 5.0

       
    • David Hall

      David Hall - 2004-09-18

      Thanks for the note: it is easier to read with the new functionality in 5.0.

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks