/** Returns the index of the first match, or -1 if no match */ findFirst(1matchFct) { let elements =2this.elements; for (let i = 0; i < elements.length; i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; }
class QueryArray { static from(elements) { return new this(elements); } constructor(elements) { this.elements = elements; } map(mapFct) { let newElements = Array.newDynamic(); for (let nextElem of this.elements) { let newElem = mapFct(nextElem); newElements.add(newElem); } return QueryArray.from(newElements); } forEach(actionFct) { for (let nextElem of this.elements) actionFct(nextElem); } findFirst(matchFct) { let elements = this.elements; for (let i = 0; i < elements.length; i++) { let nextElem = elements.get(i); if (matchFct(nextElem)) return i; } return -1; } toString() { return this.elements.toString(); } printAll(label) { println(label); this.forEach(nextElem => println(nextElem.toString())); } } _br_ _br_//Try It - map into array of countries let array = Mathematician.1newSampleSet1(); let qarray = QueryArray.2from(array); qarray.printAll(); let matchIndex = qarray.3findFirst(next => next.getCountry().equals('Kenya'));4println("\nIndex of first match: " + matchIndex);