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)