6

I am trying to add and object to a list not just a number so when you reply with an example if you could use an object like my used car example or a fruit or something I've been looking every where and all the examples i see are just adding numbers to a list.
Im trying to convert some java code into scala the java code im having trouble converting is

ArrayList usedCarList = new ArrayList();
UsedCars usedCar = new UsedCars();
usedCarList.add(usedCar);

Now i've looked at a few examples but they dont seem to work once i start trying to use an object ie

var b = List[Int](); 
b ::= 1; 
b ::= 2; 
b ::= 3; 

I've tried a few things 1 is listed below.

var usedCarList = List();
def addCar (usedCarList: List[UsedCars]){
var usedCar = new UsedCars();
series of set operations on the usedCar
usedCar :: usedCarList;
println(usedCarList.length);
}

when i check the size of the list its always empty

Jeremy
  • 935
  • 5
  • 18
  • 33
  • Also i dont care where it inserts it into the list as its location doesnt matter to me. – Jeremy Apr 01 '13 at 20:00
  • From the comments, the above isn't how you are _using_ the code -- instead of that, you declare a `var` and pass it as a parameter for something else to change it, right? Please, edit the question to state what you are actually doing. – Daniel C. Sobral Apr 01 '13 at 20:44
  • sorry i edited the main question to follow how my code actually looks. So yes i create the list in a main method and pass it to this addCar function where usedCars get added to the list. Similarly I have a removeCar function that will remove cars from the list. – Jeremy Apr 01 '13 at 20:48

2 Answers2

8

There are mutable (such as scala.collection.mutable.MutableList) and immutable lists (scala.collection.immutable.List). What you're using are immutable lists, so by just calling :: on an element actually returns a new instance with the added element, but doesn't change the underlying value. You'd have to either use a var with an immutable list, or use a mutable one like this:

scala> import scala.collection._
import scala.collection._

scala> val list = mutable.MutableList[UsedCars]()
list: scala.collection.mutable.MutableList[UsedCars] = MutableList()

scala> list += new UsedCars()
res0: list.type = MutableList(UsedCars@4bfa79c8)

scala> list.size
res1: Int = 1

See su-'s answer for reassigning the reference with the immutable lists.

Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
  • how does this work when passing the list around to functions? is the paremeter in the function just a List? – Jeremy Apr 01 '13 at 20:24
  • You can call `toList` on an instance of `MutableList`, which converts it to an instance of `List` that you can pass around. See this for more on `MutableList` and alternatives: http://stackoverflow.com/questions/5446744/difference-between-mutablelist-and-listbuffer – Alex Yarmula Apr 01 '13 at 20:28
  • I guess a better clarification of how my code is organized will help to determine how to make this work. my main method is where the list is created it is passed to the function addCar where a car is added to the list. Other functions are used to display the usedcars in the list and to remove cars from the list. which is easier to do this using the mutable or inmutable list? so if i followed the mutable example what would the addCars function look like – Jeremy Apr 01 '13 at 20:36
  • If you wanted to use mutable lists for this, I suggest creating a wrapper class around the list. It'll keep the reference to the list and have your methods, such as addCar, removeCar. – Alex Yarmula Apr 01 '13 at 20:42
6

There's a fundamental distinction between Scala's List and Java's ArrayList: Scala's List is immutable.

Not simply read only -- a read only collection in Java may still be changed by whoever created it. In Scala, a List cannot be changed by anyone.

Now, let's reconcile that with the example you showed that "works": b ::= 1. That example is equivalent to b = 1 :: b, so it isn't changing the list. Instead, it is creating a new list, and assigning it to b. That may sound inefficient, but it is actually quite fast because List is a persistent data structure (look that up for more information).

The most obvious answer to your question, therefore, is to use a different kind of data structure. Scala's closest equivalent to ArrayList is the ArrayBuffer.

However, there's another possibility that may be pursued. Instead of changing the list and returning nothing, return a new list. That may well require other changes to the structure of the code, and since there's no detail about it, I won't speculate on what they might be.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681