scala(13)-----集合(Collection)-------元组
来源:互联网 发布:mathematics软件 编辑:程序博客网 时间:2024/05/22 14:03
与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素。
元组的值是通过将单个的值包含在圆括号中构成的。例如:
val t = (1, 3.14, "Fred")
以上实例在元组中定义了三个元素,对应的类型分别为[Int, Double, java.lang.String]。
此外我们也可以使用以上方式来定义:
val t = new Tuple3(1, 3.14, "Fred")
元组的实际类型取决于它的元素的类型,比如 (99, "runoob") 是 Tuple2[Int, String]。 ('u', 'r', "the", 1, 4, "me") 为 Tuple6[Char, Char, String, Int, Int, String]。
目前 Scala 支持的元组最大长度为 22。对于更大长度你可以使用集合,或者扩展元组。
访问元组的元素可以通过数字索引,如下一个元组:
val t = (4,3,2,1)
我们可以使用 t._1 访问第一个元素, t._2 访问第二个元素,如下所示:
object Test { def main(args: Array[String]) { val t = (4,3,2,1) val sum = t._1 + t._2 + t._3 + t._4 println( "元素之和为: " + sum ) }}执行以上代码,输出结果为:
元素之和为: 10
迭代元组
你可以使用 Tuple.productIterator() 方法来迭代输出元组的所有元素:
object Test { def main(args: Array[String]) { val t = (4,3,2,1) t.productIterator.foreach{ i =>println("Value = " + i )} }}
执行以上代码,输出结果为:
Value = 4Value = 3Value = 2Value = 1
元组转为字符串
你可以使用 Tuple.toString() 方法将元组的所有元素组合成一个字符串,实例如下:
object Test { def main(args: Array[String]) { val t = new Tuple3(1, "hello", Console) println("连接后的字符串为: " + t.toString() ) }}
执行以上代码,输出结果为:
连接后的字符串为: (1,hello,scala.Console$@4dd8dc3)
元素交换
你可以使用 Tuple.swap 方法来交换元组的元素。如下实例:
object Test { def main(args: Array[String]) { val t = new Tuple2("www.google.com", "www.runoob.com") println("交换后的元组: " + t.swap ) }}
执行以上代码,输出结果为:
交换后的元组: (www.runoob.com,www.google.com)
文章来源:http://www.runoob.com/scala/scala-tuples.html
阅读全文
0 0
- scala(13)-----集合(Collection)-------元组
- Scala 集合(Collection)
- scala(13)-----集合(Collection)-------列表
- scala(13)-----集合(Collection)-------Set(集合)
- scala(13)-----集合(Collection)-------Map(映射)
- Scala之集合Collection
- Scala Immutable Collection(集合)
- Scala中的数组、映射、元组、集合
- Scala之Collection(三)
- 集合(Collection)
- Java 集合(Collection)
- 集合框架(Collection)
- Java 集合(Collection)
- JAVA--集合(Collection)
- 集合(collection API)
- 集合框架(collection)
- C# 集合(Collection)
- 集合(Collection)
- 【转】nodejs全局安装和本地安装的区别
- Android Webview 详解
- VS2012远程调试
- 在java中使用word来简单进行分词
- SpringBoot+AngularJS+Jaspersoft Studio实现多张报表打印控制
- scala(13)-----集合(Collection)-------元组
- 移动端事件详解
- http握手协议
- LeetCode 633 : Sum of Square Numbers(python)
- Oryx-Batch层解析
- Java必须理解的六大基础问题
- pycharm快捷键的用法
- 解决glide不能正常设置tag,以及listview子item的索引
- 列表练习