Scala 学习笔记之集合(6)

mac2022-06-30  61

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) } }

运行结果:

whiteblueyellowList(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, ?)

转载于:https://www.cnblogs.com/AK47Sonic/p/7087692.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)