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

相关文章:

  • 2022-12-23
  • 2021-10-17
  • 2021-10-28
  • 2022-02-14
  • 2021-06-02
  • 2020-02-18
  • 2021-05-23
  • 2021-08-24
猜你喜欢
  • 2021-08-22
  • 2021-12-05
  • 2021-06-21
  • 2021-11-14
  • 2021-08-14
  • 2021-08-31
  • 2021-10-24
相关资源
相似解决方案