Class EnhancedForLoopExamples
java.lang.Object
net.jrodolfo.java_evolution.java05.EnhancedForLoopExamples
Demonstrates the enhanced
for loop introduced in Java 5.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMinimal custom iterable used by the enhanced-for examples. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionreleaseNames(String first, String second) Creates a small customIterableto show how application classes can become enhanced-for friendly.intsumArray(int[] values) Sums an array using enhancedfor; the feature works for arrays as well asIterablevalues.inttotalLengthWithEnhancedFor(Iterable<String> names) Counts total characters using the enhancedforloop.inttotalLengthWithIterator(Iterable<String> names) Counts total characters using the older explicit iterator style.
-
Constructor Details
-
EnhancedForLoopExamples
public EnhancedForLoopExamples()
-
-
Method Details
-
totalLengthWithEnhancedFor
Counts total characters using the enhancedforloop.The enhanced
forloop only needs anIterable; callers can pass aList, aSet, or their own iterable type without converting it to a narrower collection type.- Parameters:
names- names to inspect- Returns:
- total character count
-
totalLengthWithIterator
-
sumArray
public int sumArray(int[] values) Sums an array using enhancedfor; the feature works for arrays as well asIterablevalues.- Parameters:
values- values to sum- Returns:
- the sum
-
releaseNames
-