Kotlin - Pair、Triple
公開日:2019-11-06 更新日:2019-11-06
[Kotlin]
1. 概要
Pair は、タプル(Tuple)のように、2つの値を保持できます。
Triple は、3つの値を保持できます。
Triple は、3つの値を保持できます。
2. Pair
Pair を使うと、関数の戻り値を2つ返すことができます。
fun test():Pair<Int, String> {
return Pair(1, "abc")
}
val (a, b) = test()
println(a) // 1
println(b) // abc
val result = test()
println(result.first) // 1
println(result.second) // abc
3. Triple
Triple を使うと、関数の戻り値を3つ返すことができます。
fun test() = Triple(1, 2, "abc")
val (a, b, c) = test()
println(a) // 1
println(b) // 2
println(c) // abc