建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class School { public List<String> ls = null; public School() { ls = new ArrayList<String>(); ls.add("abc"); ls.add("efg"); } } im…
import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { //可变集合类型推导 val nums = Buffer(1) for (i <- 2 to 10) nums += i println(nums) //可变集合类型定义 val nums2 = Buffer[Int]() for (i <- 1 to 10) nums2 += i println(n…
集合的模式匹配操作: object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List(1, 2, 3, 4, 5) println( ls match { case List(1, 2, 3) => "List(1,2,3)" case List(1, 2, 3, 4) => "List(1,2,3,4)" case List(1, 2, 3…
object CollectionDemo10 { def main(args: Array[String]): Unit = { var ls = List[Int](1, 2, 3) //向后增加元素 ls = ls :+ 4 //向前增加元素 ls = 0 +: ls //基本等价 ls = -1 :: ls //向前增加集合 ls = List(-2) ++: ls //基本等价 ls = List(-2) ::: ls //向后增加集合 ls = ls ++ List(5) print…