object CollectionDemo7 {
def main(args: Array[String]): Unit = {
//数组使用
val arr = Array("red", "blue", "yellow")
arr(0) = "white"
for(el <- arr){println(el)}
//用Seq构建List
println(Seq("red", "blue", "yellow"))
//用IndexedSeq构建Vector
println(IndexedSeq("red", "blue", "yellow"))
//构建Stream lazy集合
def inc(i: Int): Stream[Int] = Stream.cons(i, inc(i+1))
val s = inc(1)
println(s)
println(s.take(10).toList)
println(s) def addHead(i: Int): Stream[Int] = i #:: addHead(i+1)
val ss = addHead(1)
println(ss)
println(ss.take(10).toList)
println(ss) }
}
运行结果:
white
blue
yellow
List(red, blue, yellow)
Vector(red, blue, yellow)
Stream(1, ?)
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ?)
Stream(1, ?)
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ?)