Tuple可以作为集合存储不同类型的数据,初始化实例如下:
val tuple = (1,3,3.14,"aa")
val third = tuple._3
Tuple 下标访问从1开始, 需要用下划线。
Scala函数还可以返回Tuple类型的值,并接收,如:
val (first,second,third,forth) = tuple
跟Tuple无关但是挺有意思的东西,顺便写在这里:
zip方法,(拉链操作)
val pa = Array(1,2,3) //> pa : Array[Int] = Array(1, 2, 3)
val pb = Array("a","b", "c") //> pb : Array[String] = Array(a, b, c)
val pair = pa.zip(pb) //> pair : Array[(Int, String)] = Array((1,a), (2,b), (3,c))
时间: 2024-11-07 08:42:07