views:

104

answers:

3

What type in Scala 2.8 can I use to store a list of values? In C# I'd use ArrayList.

+4  A: 

You use ArrayBuffer

David Pierre
+4  A: 

You could use ArrayBuffer.

You can see other mutable collections here: scala.collections.mutable

NullUserException
I've seen that list, but it features not very informative descriptions :-(
Ivan
@Ivan If you click on the links, they will take you to a page with more informative descriptions ;-)
NullUserException
@nulluserexception I mean those pages with "more informative descriptions" have still not very informative descriptions.
Ivan
+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
@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
@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
@rex-kerr thanks a lot. I hope someday documentation is going to be such descriptive.
Ivan
@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