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

发布于 2024-12-10 01:15:46 字数 174 浏览 0 评论 0原文

我无法将 T 类型的元素添加到列表 List[T] 中。 我尝试使用 myList ::= myElement 但它似乎创建了一个奇怪的对象,并且访问 myList.last 总是返回放入列表中的第一个元素。我该如何解决这个问题?

I can't add an element of type T into a list List[T].
I tried with myList ::= myElement but it seems it creates a strange object and accessing to myList.last always returns the first element that was put inside the list. How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娇妻 2024-12-17 01:15:46
List(1,2,3) :+ 4

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

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

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).

哭了丶谁疼 2024-12-17 01:15:46

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

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

或:

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

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)
唔猫 2024-12-17 01:15:46

Scala 中的列表不适合修改。事实上,你不能向 Scala 添加元素 <代码>列表;它是一个不可变的数据结构,就像 Java 字符串一样。
当您在 Scala 中“向列表中添加元素”时,您实际上所做的是从现有列表创建一个新列表。 (来源)

而不是对于此类用例使用列表,我建议使用 ArrayBufferListBuffer。这些数据结构旨在添加新元素。

最后,完成所有操作后,可以将缓冲区转换为列表。请参阅以下 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)

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)

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

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

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)
攀登最高峰 2024-12-17 01:15:46

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

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)

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)
怂人 2024-12-17 01:15:46

我们可以追加或添加两个列表或列表和数组
追加:

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

前置:

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

Result:6 5 4 1 2 3

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

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
东京女 2024-12-17 01:15:46

Scala 中的列表是不可变的

您可以使用 MutableList

var l = scala.collection.mutable.MutableList(1,2,3)
l += 4 

List is immutable in Scala

You can use MutableList

var l = scala.collection.mutable.MutableList(1,2,3)
l += 4 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文