Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding an object to list in scala

Tags:

scala

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

like image 226
Jeremy Avatar asked Apr 01 '13 19:04

Jeremy


2 Answers

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.

like image 107
Alex Yarmula Avatar answered Sep 18 '22 10:09

Alex Yarmula


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.

like image 31
Daniel C. Sobral Avatar answered Sep 18 '22 10:09

Daniel C. Sobral