在 Scala 中将元素附加到列表的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7794063/
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 03:35:47  来源:igfitidea点击:

Appending an element to the end of a list in Scala

arraysscala

提问by Masiar

It sounds like a stupid question, but all I found on the internet was trash. I simply can't add an element of type Tinto a list List[T]. I tried with myList ::= myElementbut it seems it creates a strange object and accessing to myList.lastalways returns the first element that was put inside the list.

这听起来像是一个愚蠢的问题,但我在互联网上发现的都是垃圾。我根本无法将 type 元素添加T到 list 中List[T]。我尝试过,myList ::= myElement但它似乎创建了一个奇怪的对象,并且访问myList.last总是返回放在列表中的第一个元素。

回答by Landei

List(1,2,3) :+ 4

Results in List[Int] = List(1, 2, 3, 4)

Note that this operation has a complexity of O(n). If you need this operation frequently, or for long lists, consider using another data type (e.g. a ListBuffer).

请注意,此操作的复杂度为 O(n)。如果您经常需要此操作,或者对于长列表,请考虑使用其他数据类型(例如 ListBuffer)。

回答by agilesteel

That's because you shouldn't do it (at least with an immutable list). If you really really need to append an element to the end of a data structure and this data structure really really needs to be a list and this list really really has to be immutable then do eiher this:

那是因为您不应该这样做(至少对于不可变列表)。如果你真的需要将一个元素附加到数据结构的末尾,并且这个数据结构真的需要是一个列表,并且这个列表真的必须是不可变的,那么请执行以下操作:

(4 :: List(1,2,3).reverse).reverse

or that:

或者那个:

List(1,2,3) ::: List(4)

回答by Markus Weninger

Lists in Scala are not designed to be modified. In fact, you can't add elements to a Scala List; it's an immutable data structure, like a Java String. What you actually do when you "add an element to a list" in Scala is to create a new List from an existing List. (Source)

Scala 中的列表不是为修改而设计的。实际上,您不能向 Scala 中添加元素List;它是一个不可变的数据结构,就像 Java 字符串一样。当您在 Scala 中“向列表中添加元素”时,您实际做的是从现有的 List创建一个新的 List(来源)

Instead of using lists for such use cases, I suggest to either use an ArrayBufferor a ListBuffer. Those datastructures are designed to have new elements added.

我建议不要在此类用例中使用列表,而是使用 anArrayBuffer或 a ListBuffer。这些数据结构旨在添加新元素。

Finally, after all your operations are done, the buffer then can be converted into a list. See the following REPL example:

最后,在您完成所有操作后,可以将缓冲区转换为列表。请参阅以下 REPL 示例:

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> var fruits = new ListBuffer[String]()
fruits: scala.collection.mutable.ListBuffer[String] = ListBuffer()

scala> fruits += "Apple"
res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple)

scala> fruits += "Banana"
res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana)

scala> fruits += "Orange"
res2: scala.collection.mutable.ListBuffer[String] = ListBuffer(Apple, Banana, Orange)

scala> val fruitsList = fruits.toList
fruitsList: List[String] = List(Apple, Banana, Orange)

回答by Venkat

This is similar to one of the answers but in different way :

这类似于其中一个答案,但方式不同:

scala> val x=List(1,2,3)
x: List[Int] = List(1, 2, 3)

scala> val y=x:::4::Nil
y: List[Int] = List(1, 2, 3, 4)

回答by Ramesh Muthavarapu

We can append or prepend two lists or list&array
Append:

我们可以附加或前置两个列表或 list&array
附加:

var l = List(1,2,3)    
l=l:+4 
Result : 1 2 3 4  
var ar = Array(4,5,6)    
for(x<-ar)    
{ l=l:+x}  
  l.foreach(println)

Result:1 2 3 4 5 6

Prepending:

前置:

var l = List[Int]()  
   for(x<-ar)  
    { l=x::l } //prepending    
     l.foreach(println)   

Result:6 5 4 1 2 3