No Scala mutable list

强颜欢笑 提交于 2019-12-10 12:37:44

问题


Scala has both a mutable and an immutable Map , but it has only an immutable List. If you want a mutable List you need a ListBuffer.

I don't understand why this is so. Any one knows?.


回答1:


You can choose between these:

  • scala.collection.mutable.DoubleLinkedList
  • scala.collection.mutable.LinkedList
  • scala.collection.mutable.ListBuffer
  • scala.collection.mutable.MutableList

So, yes, Scala has mutable lists :-)




回答2:


I hope that this article may be of some use to you. The diagram at the bottom of the page is particularly useful in providing the mutable and immutable classes.

http://www.scala-lang.org/docu/files/collections-api/collections_1.html




回答3:


There is a mutable List, but it is called Buffer. The article linked by Graham goes into more depth, but I thought there should be a specific answer to the question as well.




回答4:


Map is a trait -- like Java's interface --, while List is a class, a concrete implementation of a Seq. There are mutable and immutable Seq, just like for Map.

This may be confusing to Java programmers because, in Java, List is an interface, whose (main) implementations are ArrayList and LinkedList. Alas, Java naming is atrocious. First, ArrayList is not a List by any stretch of imagination. Also, the interface has methods that are not really related to any traditional list.

So, if you want mutable/immutable equivalence, look to concrete subclass implementations of Seq.



来源:https://stackoverflow.com/questions/6706060/no-scala-mutable-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!