/*-*********************************************************************** TEMPLATE-ANALYSIS: return-type method-name(ArrayList<T> alist){ int index; // to represent the traversal return-type acc = BASE-VALUE; for (index = 0; // start the traversal at the beginning CONTINUATION-PREDICATE; index = ADVANCE){ acc = UPDATE (CURREENT, acc); } return acc; } COMPLETE METHOD TEMPLATE: ------------------------- <T> return-type method-name(ArrayList<T> alist){ +------------------------------+ | return-type acc = BASE-VALUE |; +------------------------------+ for (index = 0; +----------------------+ | index < alist.size() |; +----------------------+ +-----------+ index = | index + 1 |) +-----------+ { +-------------------------------+ acc = | update(alist.get(index), acc) |; +-------------------------------+ } return acc; } <T> return-type update(T t, return-type acc){ ... }
/** IMPERATIVE VERSION THAT USES for LOOP WITH index based traversal. * Count how many data elements in thre given ArrayList * satisfy the given ISelect predicate. */ public <T> boolean orMapForCounted(ArrayList<T> alist, ISelect<T> choice){ // Define the accumulator and initialize it to the BASE-VALUE; boolean acc = false; // loop header: // for(... accumulator is already defined and initialized ... // ...BUT initalize the loop index: int index = 0; // continuation-predicate: index < alist.size() // update: index = index + 1 for(int index = 0; index < alist.size(); index = index + 1){ // loop body: uses current element acc = updateOrMap(alist.get(index), acc, choice); } // postmortem: produce the result return acc; }