Scala Programming Projects
上QQ阅读APP看书,第一时间看更新

Working with tuples

Type the following in the Scala Console to get more familiar with tuples. Feel free to experiment with different types and sizes of tuples:

scala> val tuple3 = (1, "hello", 2.0)
tuple3: (Int, String, Double) = (1,hello,2.0)

scala> tuple3._1
res1: Int = 1

scala> tuple3._2
res2: String = hello

scala> val (a, b, c) = tuple3

a: Int = 1
b: String = hello
c: Double = 2.0

You can create tuples of any length up to 22, and access their elements using _1, _2, and so on. You can also declare several variables in one go for each element of the tuple.