温馨提示×

scala如何获取元组的值

小亿
132
2024-06-05 14:24:19
栏目: 编程语言

在Scala中,可以通过索引来获取元组中的值。元组的索引从1开始,例如:

val tuple = (1, "hello", 3.14) val firstValue = tuple._1 val secondValue = tuple._2 val thirdValue = tuple._3 println(firstValue) // 输出: 1 println(secondValue) // 输出: hello println(thirdValue) // 输出: 3.14 

另外,也可以使用模式匹配来获取元组中的值,例如:

val tuple = (1, "hello", 3.14) tuple match { case (first, second, third) => { println(first) // 输出: 1 println(second) // 输出: hello println(third) // 输出: 3.14 } } 

0