funtransform(arr: Array<Int>, action: (index: Int, ele: Int) -> Int) { //遍历下标集 for (index in arr.indices) { val newValue = action(index, arr[index]) arr[index] = newValue } }
funmain() { val numbers = arrayOf(1,2,3,4) //传入Lambda: 将数组的每个元素与其下标相乘 //效果与 numbers.mapIndexed{index, value -> index * value} 相当 transform(numbers, {index, ele -> index * ele })