scala有一个使用很方便的数据类型叫做元组,元组的好处就是可以容纳的数据类型很多,和前端js中的数组一样,但是他是有长度限制的,最小是二元,最长大概是24左右,而且他的数据不可变
package com.wy
object Tuple {
def main(args: Array[String]): Unit = {
//可以这样写:new Tuple2("","")
var t = (1, 3.14, "Fred")
println(t._1)
println(t._2)
println(t._3)
t.productIterator.foreach{ i =>println("Value = " + i )}
//二元组
var tuple = ("name","zhangsan")
var tuple2 = ("age",18)
var tuple3 = ("sex","男")
//"name","zhangsan"
//"age",18
//"sex","男"
var tuples = List(("name", "zhangsan"), ("age", 18), ("sex", "男"))
for(tup <- tuples){
println(tup._1+":"+tup._2)
}
}
}