将对象添加到 Scala 中的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15750759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:07:38  来源:igfitidea点击:

adding an object to list in scala

scala

提问by Jeremy

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

我试图在列表中添加和反对,而不仅仅是一个数字,所以当你回复一个例子时,如果你可以使用一个对象,比如我的二手车例子或水果或我一直在寻找的任何地方和我看到的所有例子只是将数字添加到列表中。
我正在尝试将一些 Java 代码转换为 Scala 转换时遇到问题的 Java 代码是

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.

我已经尝试了一些事情 1 下面列出。

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

当我检查列表的大小时,它总是空的

采纳答案by Daniel C. Sobral

There's a fundamental distinction between Scala's Listand Java's ArrayList: Scala's Listis immutable.

ScalaList和 Java之间有一个根本区别ArrayList:ScalaList是不可变的。

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

不仅仅是只读——Java 中的只读集合仍然可以被创建它的人更改。在 Scala 中,任何人List都不能更改a 。

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 newlist, and assigning it to b. That may sound inefficient, but it is actually quite fast because Listis a persistent data structure (look that up for more information).

现在,让我们来调和与这个例子中你发现,“作品”: b ::= 1。该示例等效于b = 1 :: b,因此它不会更改列表。相反,它正在创建一个列表,并将其分配给b. 这听起来效率低下,但实际上相当快,因为​​它List是一个持久的数据结构(查看更多信息)。

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

因此,对您的问题最明显的答案是使用不同类型的数据结构。Scala 最接近的等价物ArrayListArrayBuffer.

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.

然而,还有另一种可能。不是更改列表并且不返回任何内容,而是返回一个新列表。这很可能需要对代码结构进行其他更改,而且由于没有关于它的详细信息,我不会推测它们可能是什么。

回答by Alex Yarmula

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 varwith an immutable list, or use a mutable one like this:

有可变的(例如scala.collection.mutable.MutableList)和不可变的列表(scala.collection.immutable.List)。您使用的是不可变列表,因此仅调用::元素实际上会返回一个包含添加元素的新实例,但不会更改基础值。您必须将 avar与不可变列表一起使用,或者使用像这样的可变列表:

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.

请参阅 su- 的答案以使用不可变列表重新分配引用。