scala集合三大類(seq序列,set集,map映射)——list序列

scala集合三大類(seq序列,set集,map映射)es6

seq序列:app

scala> val list1 = List(1,2,3)es5

list1: List[Int] = List(1, 2, 3)spa

 

scala> val list2 = 0 :: list1scala

list2: List[Int] = List(0, 1, 2, 3)io

 

//下面兩個方式效果同樣, :: and +:  table

scala> val list3 = list1.::(0)import

list3: List[Int] = List(0, 1, 2, 3)List

scala> val list4 = 0 +: list1map

list4: List[Int] = List(0, 1, 2, 3)

scala> val list5 = list1.+:(0)

list5: List[Int] = List(0, 1, 2, 3)

 

scala> val list5 = list1.+:(0)

list5: List[Int] = List(0, 1, 2, 3)

 

scala> val list6 = list1 :+ 4

list6: List[Int] = List(1, 2, 3, 4)

 

scala> val list7 = List(5,6,7)

list7: List[Int] = List(5, 6, 7)

 

scala> val list8 = list1 ++ list7

list8: List[Int] = List(1, 2, 3, 5, 6, 7)

 

scala> val list8 = list7 ++ list1

list8: List[Int] = List(5, 6, 7, 1, 2, 3)

 

scala> val list9 = list1 ::: list7

list9: List[Int] = List(1, 2, 3, 5, 6, 7)

 

scala> val list9 = list7 ::: list1

list9: List[Int] = List(5, 6, 7, 1, 2, 3)

 

可變長度list

scala> import scala.collection.mutable.ListBuffer

import scala.collection.mutable.ListBuffer

 

scala> val list1 = ListBuffer(1,2,3)

list1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

 

scala> list1 += 4                      //list列表追加元素

res54: list1.type = ListBuffer(1, 2, 3, 4)

 

scala> list1

res55: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)

 

scala> list1.append(5)                 //list列表追加元素

res56: list1.type = ListBuffer(1, 2, 3, 4, 5)

 

scala> list1

res57: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5)

 

將兩個List追加到一個List裏面

scala> val list2 = ListBuffer(6,7,8)

list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(6, 7, 8)

 

scala> list1 ++= list2                //使用 ++= 操做符能夠將後面list2的內容追加到list1中

res58: list1.type = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8)

 

scala> list1           //list1已經把list2裏面的內容追加起來了

res59: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8)

 

scala> val list4 = ListBuffer(1,2,3)

list4: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

 

scala> val list5 = ListBuffer(4,5,6)

list5: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 5, 6)

 

scala> list4 ++ list5                       //++操做符不能夠將list5的內容追加到list4中,只能用 ++= 操做符才能夠,如上例,這裏是把兩個list合併生成一個新的list的操做

res61: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6)

 

scala> list4

res62: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)       //list4並未有list5的內容

 

scala> val list6 = ListBuffer(1,2,3)

list6: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

 

scala> list6 :+ 4           //這裏雖然使用了一個不可變list的操做符能夠執行成功,可是依然是生成一個新的 list , list6並無把4添加到列表中,而是生成新的一個列表

res63: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)

 

scala> list6

res64: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

 

scala> res63

res65: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)   //使用不可變列表的操做符 :+ 將列表裏面元素與新元素合而且生成一上新的列表

相關文章
相關標籤/搜索