说明
- 元组最多有22个元素。
创建元组
val tuple1: (String, Int, String, String) = ("zhangsan", 21, "man", "beijing")
访问元组中的元素
println(tuple1._1)
println(tuple1._2)
println(tuple1._3)
println(tuple1._4)
通过索引访问
println(tuple1.productElement(1))
遍历元组
for (elem <- tuple1.productIterator) {
println(elem)
}
对偶元组
- 元组中的元素只有两个。
val tuple2: (String, String) = ("1001","zhangsan")
println(tuple2)