天天看點

scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections

http://docs.scala-lang.org/overviews/collections/overview.html

個人學習筆記,本人初學程式設計語言,力争第一遍能了解scala基本庫中的API的作用并發現不了解的問題,争取再深入學習時能夠有針對性的進行研究和學習。水準有限難免會有了解錯誤。如有讀者發現錯誤或者有問題可以留言指教。希望大家不吝賜教!文中會引用他人的部落格的連接配接,如遇版權問題請聯系我,立刻糾正。感謝前輩們的寶貴總結。

Scala collections systematically distinguish between mutable and immutable collections. A mutablecollection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You have still operations that simulate additions, removals, or updates, but those operations will in each case return a new collection and leave the old collection unchanged.

scala中有兩種集合:可變的和不可變的。這裡的可變是指集合的reference可以改變,不可變亦是如此。可以認為就是這個變量所擁有的指針的值不可變。但是集合中的元素是可變的,如下圖:

scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections
scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections

當一個集合(這裡以數組為例)初始化後,它所指向的記憶體空間就固定了(假設是固定長度的數組)。也就是說這個數組在記憶體中的位置不可變了。但是數組中每個元素的值是可以改變的,如a(0)=4後,數組的第一個元素的值就變成了4。我們再看一下改變a的值會發生什麼:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a = Array(1,2,3,4)
<console>:12: error: reassignment to val
       a = Array(1,2,3,4)
         ^
           

可以看見,當試圖給a重新劃分記憶體時候(也就是為a重新記憶體的位址的時候,報錯了,val類型的變量不能再指派)。

scala> var b = Array(1,2,3)
b: Array[Int] = Array(1, 2, 3)

scala> b = Array(1,2,3,4)
b: Array[Int] = [[email protected]

scala> b
res0: Array[Int] = Array(1, 2, 3, 4)
           

可以看見var申明的變量可以重新指派。val 和 var 可以對應 immutable 和 mutable 了解。

All collection classes are found in the package 

scala.collection

 or one of its sub-packages

mutable

immutable

, and 

generic

.

scala中所有的集合類都在scala.collection這個包下面(mutable,immutable和generic)。

By default, Scala always picks immutable collections. For instance, if you just write 

Set

 without any prefix or without having imported 

Set

 from somewhere, you get an immutable set, and if you write

Iterable

 you get an immutable iterable collection, because these are the default bindings imported from the 

scala

 package. To get the mutable default versions, you need to write explicitly

collection.mutable.Set

, or 

collection.mutable.Iterable

.

A useful convention if you want to use both mutable and immutable versions of collections is to import just the package 

collection.mutable

.

  1. import scala.collection.mutable

Then a word like 

Set

 without a prefix still refers to an immutable collection, whereas 

mutable.Set

refers to the mutable counterpart.

預設情況下,scala優先使用immutable集合,如果想使用可變的集合,請明确導入

collection.mutable.Set

collection.mutable.Iterable

來使用可變Set和Iterable集合。

scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections

scala.collection package

scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections

scala.collection.immutable package

scala:Guides and Overviews閱讀筆記之一 -- Mutable and Immutable Collections

scala.collection.mutable package