var elements, a elements = [10, 20, 30, 40] a = MyArray.from(elements); println(a) println('indexOf(30): ' + a.indexOf(30)); println('indexOf(99): ' + a.indexOf(99)); // elements: 10,20,30,40 // indexOf(30): 2 // indexOf(99): -1
// indexOf(anElem) let i = 0 while (i < this.size()) { if (this.get(i).equals(anElem)) return i i++ } return -1
var elements, a elements = [10, 20, 30] a = MyArray.from(elements) println(a) println('join("|"): ' + a.join("|")) // elements: 10,20,30 // join("|"): 10|20|30
// join(separator) var i, result i = 0 result = ""; while (i < this.size()) { let nextElem = this.get(i) if (i > 0) result += separator; result += nextElem.toString(); i++ } return result;
var a, searchFct a = new MyArray([10, 20, 30, 40, 50, 60, 70]) searchFct = anElem => anElem > 50 println(a.findIndex(searchFct)) // outputs 5 a = new MyArray([10, 50, 10, 40, 50, 60, 10]) searchFct = anElem => anElem == 50 println('----') println(a.findIndex(searchFct)) // outputs 1
// findIndex(searchFct) var i i = 0 while (i < this.size()) { if (searchFct(this.get(i))) return i; i++ } return -1;
var a, searchFct a = new MyArray([10, 20, 30, 40, 50, 60, 70]) searchFct = anElem => anElem < 50 println(a.findLastIndex(searchFct)) // outputs 3 a = new MyArray([10, 50, 10, 40, 50, 60, 10]) searchFct = anElem => anElem == 50 println('----') println(a.findLastIndex(searchFct)) // outputs 4
// findLastIndex(searchFct) var i i = this.size() - 1; while (i >= 0) { if (searchFct(this.get(i))) return i; i-- } return -1;
var a, count, matchFct a = new MyArray([10, 50, 10, 40, 50, 60, 10]) count = 0 //match if between 20 and 50 (inclusive) matchFct = anElem => anElem >= 20 && anElem <= 50 println(a) println('occurrences between 20 and 50 (inclusive)') println('occurrences(matchFct): ' + a.occurrences(matchFct));
// occurrences(matchFct) var i, count i = 0 count = 0 while (i < this.size()) { var nextElem, match nextElem = this.get(i) match = matchFct(nextElem) if (match) count++ i++ } return count;