1、List的創(chuàng)建
Scala 列表類似于數(shù)組,它們所有元素的類型都相同,但是它們也有所不同:列表是不可變的,值一旦被定義了就不能改變,其次列表 具有遞歸的結(jié)構(gòu)(也就是鏈接表結(jié)構(gòu))而數(shù)組不是。。
列表的元素類型 T 可以寫成 List[T]。例如,以下列出了多種類型的列表:
val site:List[String] = List("Runoob","Baidu","Google")
val nums:List[Int] = List(1,2,3,4)
val empty:List[Nothing] = List()
val dim:List[List[Int]] = List(
List(1,0,0),List(0,1,0),List(0,0,1)
)
當(dāng)然,我們也可以通過::和Nil進行List的創(chuàng)建
val site1 = "Runoob" :: ("Baidu" :: ("Google" :: Nil))
val nums1 = 1 :: (2 :: (3 :: (4 :: Nil)))
val empty1 = Nil
val dim1 = (1 :: (0 :: (0 :: Nil))) ::
(0 :: (1 :: (0 :: Nil))) ::
(0 :: (0 :: (1 :: Nil))) :: Nil
2、List元素獲取及遍歷
要想打印整個List,直接使用println方法即可,如果想要遍歷list,可以使用for循環(huán)。同時我們可以使用 (n)或者apply(n)來訪問List的第n個元素。
println(site(0))
println(dim(1)(1))
println(site.apply(0))
println(dim.apply(1)(1))
println(site)
for(i <- site){
println(i)
}
輸出為:
Runoob
1
Runoob
1
List(Runoob, Baidu, Google)
Runoob
Baidu
Google
我們也可以使用特殊的方法來訪問List中的元素。比如:
head 返回列表第一個元素
tail 返回一個列表,包含除了第一元素之外的其他元素
last 返回最后第一個元素
isEmpty 在列表為空時返回true
println( "第一網(wǎng)站是 : " + site.head )
//tail返回除第一個元素外的所有列表元素
println("除第一網(wǎng)站外剩下網(wǎng)站為" + site.tail)
println( "最后一個網(wǎng)站是 : " + site.last )
println( "查看列表 site 是否為空 : " + site.isEmpty )
println( "查看 nums 是否為空 : " + nums.isEmpty )
輸出為:
第一網(wǎng)站是 : Runoob
除第一網(wǎng)站外剩下網(wǎng)站為List(Baidu, Google)
最后一個網(wǎng)站是 : Google
查看列表 site 是否為空 : false
查看 nums 是否為空 : false
3、List連接
你可以使用 ::: 運算符或 List.:::() 方法或 List.concat() 方法來連接兩個或多個列表。實例如下:
val site2 = "Facebook" :: ("Taobao" :: Nil)
val site_all1 = site1 ::: site2
println( "site1 ::: site2 : " + site_all1 )
val site_all2 = site1.:::(site2)
println( "site1.:::(site2) : " + site_all2 )
val site_all3 = List.concat(site1,site2)
println( "List.concat(site1, site2) : " + site_all3 )
輸出為:
site1 ::: site2 : List(Runoob, Baidu, Google, Facebook, Taobao)
site1.:::(site2) : List(Facebook, Taobao, Runoob, Baidu, Google)
List.concat(site1, site2) : List(Runoob, Baidu, Google, Facebook, Taobao)
4、使用方法創(chuàng)建List
我們可以使用 List.fill() 方法來創(chuàng)建一個指定重復(fù)數(shù)量的元素列表:
val fillsite = List.fill(3)("Runoob")
println( "site : " + fillsite )
val fillnum = List.fill(10)(2)
println( "num : " + fillnum )
輸出為:
site : List(Runoob, Runoob, Runoob)
num : List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
List.tabulate() 方法是通過給定的函數(shù)來創(chuàng)建列表。
方法的第一個參數(shù)為元素的數(shù)量,可以是二維的,第二個參數(shù)為指定的函數(shù),我們通過指定的函數(shù)計算結(jié)果并返回值插入到列表中,起始值為 0,實例如下:
val squares = List.tabulate(6)(x=>x*x)
println( "一維 : " + squares )
val mul = List.tabulate(4,5)(_ * _)
println( "多維 : " + mul )
輸出為:
一維 : List(0, 1, 4, 9, 16, 25)
多維 : List(List(0, 0, 0, 0, 0), List(0, 1, 2, 3, 4), List(0, 2, 4, 6, 8), List(0, 3, 6, 9, 12))
5、List常用方法(更新中)
reverse反轉(zhuǎn)
println( "site 反轉(zhuǎn)前 : " + site )
println( "site 反轉(zhuǎn)后 : " + site.reverse )
輸出為:
site 反轉(zhuǎn)前 : List(Runoob, Baidu, Google)
site 反轉(zhuǎn)后 : List(Google, Baidu, Runoob)
添加元素
由于List是不可變的,所以要想往List添加元素,都會返回一個新的List:
val a:List[Int] = List(1,2)
val b = 3 +: a
println(b)
val c = a :+ 3
println(c)
輸出為:
List(3, 1, 2)
List(1, 2, 3)
contains是否包含某元素
println("site 是否包含F(xiàn)aceBook:" + site.contains("FaceBook"))
輸出為:
site 是否包含F(xiàn)aceBook:false
將函數(shù)應(yīng)用到列表的所有元素
foreach 的沒有返回值的,而map返回一個新的List
c.foreach(x => x * 2)
val d = c.map(_ * 2)
println(d)
mkString:List轉(zhuǎn)換為String
println(nums.mkString(","))
輸出為:
1,2,3,4
sorted 排序
val e = b.sorted
println(e)
輸出為:
List(1, 2, 3)