What type in Scala 2.8 can I use to store a list of values? In C# I'd use ArrayList.
+4
A:
You could use ArrayBuffer
.
You can see other mutable collections here: scala.collections.mutable
NullUserException
2010-09-15 12:57:10
I've seen that list, but it features not very informative descriptions :-(
Ivan
2010-09-15 13:00:43
@Ivan If you click on the links, they will take you to a page with more informative descriptions ;-)
NullUserException
2010-09-15 13:01:49
@nulluserexception I mean those pages with "more informative descriptions" have still not very informative descriptions.
Ivan
2010-09-15 17:34:53
+7
A:
As others have pointed out, you want an ArrayBuffer
. In general, in Scala, a buffer is a resizable, mutable, linear collection of data. In addition to ArrayBuffer
, a ListBuffer
works like a C# or Java mutable list--and, in fact, JListWrapper
wraps Java's List
and works basically the same way.
A good source of documentation for the collections classes is the Collections API document. It describes the implementations in detail and suggests common use cases.
Rex Kerr
2010-09-15 13:20:02
@rex-kerr would you be so kind to explain what are the most important differences between ArrayBuffer and ListBuffer, and why should I use one of them rather than another? Your "works like a C#" says me I'd better use ListBuffer (as I came to Scala 2.8 right from C# (because I've moved to Linux)), but everyone else says I need ArrayBuffer, so I am still confused about this.
Ivan
2010-09-15 17:37:53
@Ivan - As the names suggest, `ArrayBuffer` is backed by an array, while `ListBuffer` is built from a linked list. The only time I'd use `ListBuffer` is when I needed to both append and prepend--`ListBuffer` does either one quickly--and didn't need random access (which lists do slowly). `ArrayBuffer` appends quickly, and does random access well, but since it's an array, it's slow to add a new element at the front.
Rex Kerr
2010-09-15 17:42:07
@rex-kerr thanks a lot. I hope someday documentation is going to be such descriptive.
Ivan
2010-09-15 18:17:06
@Ivan - The Collections API document has all that information, and it's fairly easy to find (though it requires one to know enough about data structures to understand the differences between a doubly linked list and an array). Just look under "mutable collections" and "performance". The Scaladocs don't give as much information, however.
Rex Kerr
2010-09-15 19:10:37