1、元组定义
与列表一样,与列表不同的是元组可以包含不同类型的元素。元组的值是通过将单个的值包含在圆括号中构成的。
2、创建元组与取值
注意:tuple最多支持22个参数
1.//创建,最多支持22个
2.val tuple = new Tuple1(1)
3.val tuple2 = Tuple2("zhangsan",2)
4.val tuple3 = Tuple3(1,2,3)
5.val tuple4 = (1,2,3,4)
6.val tuple18 = Tuple18(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
7.val tuple22 = new Tuple22(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
8.
9.//使用
10.println(tuple2._1 + "\t"+tuple2._2)
11.val t = Tuple2((1,2),("zhangsan","lisi"))
12.println(t._1._2)
tuple.productIterator得到迭代器,进而遍历
1.//遍历
2.val tupleIterator = tuple22.productIterator
3.while(tupleIterator.hasNext){
4.println(tupleIterator.next())
5.}