a
int [] a; … code to load up a for (int i = 0; i < a.length; i++) print element a[i] …
… code to load up a printA(); … void printA() { // (assuming 'a' is accessible from this procedure) for (int i = 0; i < a.length; i++) print element a[i] }
… code to load up a printA(); code to sort a printA(); …
… code to load up a printAarray(a); … void printA(int [] a) { for (int i = 0; i < a.length; i++) print element a[i] }
… code to load up a printAarray(a); … <T> void printA(T [] a) { for (int i = 0; i < a.length; i++) print element a[i] }
… code to load up a printArray(a); … void printArray(T [] a) { for (Iterator iter = a.iterator(); iter.hasNext(); ) print iter.next() }
… code to load up a printArray(a); … void print(Collection c) { for (Iterator iter = c.iterator(); iter.hasNext(); ) print iter.next() }
interface Procedure { void do(Object object); }
class Printer implements Procedure { void do(Object object) {System.out.println(object);} } … code to load up a printArray(a, new Printer()); … void print(Collection c, Procedure procedure) { for (Iterator iter = c.iterator(); iter.hasNext(); ) procedure.do(iter.next()) }
interface Procedure { void do(Object object); }
interface Predicate { boolean test(Object object); }
class Printer implements Procedure { void do(Object object) {System.out.println(object);} } class IsEven implements Predicate { boolean test(Object object) {return ((Integer)object) % 2 == 0;} } … code to load up a printArray(a, new Printer(), new IsEven()); … void print(Collection c, Procedure procedure, Predicate predicate) { for (Iterator iter = c.iterator(); iter.hasNext(); ) { Object object = iter.next(); if (predicate.test(object)) procedure.do(object); } }
class Printer implements Procedure { void do(Object object) {System.out.println(object);} } class IsEven implements Predicate { boolean test(Object object) {return ((Integer)object) % 2 == 0;} } … code to load up a a.forEach(new Printer(), new IsEven());
foreach
that performs the iteration of the previous step is also defined and made a method of
Collection
(actually Iterable
code to load up a a.forEach(new Procedure () { void do(Object object) {System.out.println(object); }, new Predicate() { boolean test(Object object) {return ((Integer)object) % 2 == 0;} });
code to load up a a.forEach(object -> System.out.println(object), object -> ((Integer)object) % 2 == 0;);
The code at the link below is working C++ and Java code, and is loosely related to the above sequence