Spread Operator 将数组传递给 vararg 函数

可以使用 Spread Operator* 将数组传递给 vararg 函数。

假设存在以下功能……

fun printNumbers(vararg numbers: Int) {
    for (number in numbers) {
        println(number)
    }
}

你可以像这样将数组传递给函数…

val numbers = intArrayOf(1, 2, 3)
printNumbers(*numbers)

// This is the same as passing in (1, 2, 3)

扩展运算符也可以参数中间使用 …

val numbers = intArrayOf(1, 2, 3)
printNumbers(10, 20, *numbers, 30, 40)

// This is the same as passing in (10, 20, 1, 2, 3, 30, 40)