Class EnhancedForLoopExamples

java.lang.Object
net.jrodolfo.java_evolution.java05.EnhancedForLoopExamples

public class EnhancedForLoopExamples extends Object
Demonstrates the enhanced for loop introduced in Java 5.
  • Constructor Details

    • EnhancedForLoopExamples

      public EnhancedForLoopExamples()
  • Method Details

    • totalLengthWithEnhancedFor

      public int totalLengthWithEnhancedFor(Iterable<String> names)
      Counts total characters using the enhanced for loop.

      The enhanced for loop only needs an Iterable; callers can pass a List, a Set, or their own iterable type without converting it to a narrower collection type.

      Parameters:
      names - names to inspect
      Returns:
      total character count
    • totalLengthWithIterator

      public int totalLengthWithIterator(Iterable<String> names)
      Counts total characters using the older explicit iterator style.
      Parameters:
      names - names to inspect
      Returns:
      total character count
    • sumArray

      public int sumArray(int[] values)
      Sums an array using enhanced for; the feature works for arrays as well as Iterable values.
      Parameters:
      values - values to sum
      Returns:
      the sum
    • releaseNames

      public Iterable<String> releaseNames(String first, String second)
      Creates a small custom Iterable to show how application classes can become enhanced-for friendly.
      Parameters:
      first - first release name
      second - second release name
      Returns:
      an iterable pair of release names