20.scala编程思想笔记——Vector
欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/50444169
源码下载连接请见第一篇笔记。
Vector是一个容器,即保存其他对象的对象。容器也称为集合。Vector是标准Scala包的一部分,不需要任何导入就可以使用。
如下:
importcom.atomicscala.AtomicTest._
// A Vector holds other objects:
val v1 = Vector(1, 3, 5, 7, 11, 13)
v1 is Vector(1, 3, 5, 7, 11, 13)
v1(4) is 11 // "Indexing" into a Vector
// Take each element of the Vector:
var result = ""
for(i <- v1) {
result += i +" "
}
result is "1 3 5 7 11 13 "
val v3 = Vector(1.1, 2.2, 3.3, 4.4)
// reverse is an operation on the Vector:
v3.reverse is Vector(4.4, 3.3, 2.2, 1.1)
var v4 = Vector("Twas","Brillig", "And",
"Slithy", "Toves")
v4 is Vector("Twas", "Brillig","And",
"Slithy", "Toves")
v4.sorted is Vector("And","Brillig",
"Slithy", "Toves", "Twas")
v4.head is "Twas"
v4.tail is Vector("Brillig","And",
"Slithy", "Toves")
Scala允许我们构建无需使用new就可以被实例化的类。
[root@localhost examples]# scala -nocompdaemonVectors.scala
Vector(1, 3, 5, 7, 11, 13)
11
1 3 5 7 11 13
Vector(4.4, 3.3, 2.2, 1.1)
Vector(Twas, Brillig, And, Slithy, Toves)
Vector(And, Brillig, Slithy, Toves, Twas)
Twas
Vector(Brillig, And, Slithy, Toves)
关于Vector的更多信息,可以查看ScalaDoc。