scala 附加到可变 LinkedList

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

scala append to a mutable LinkedList

scalacollectionsmutable

提问by weima

Please check this

请检查这个

import scala.collection.mutable.LinkedList

var l = new LinkedList[String]

l append LinkedList("abc", "asd")

println(l)
// prints 
// LinkedList()

but

import scala.collection.mutable.LinkedList

var l = new LinkedList[String]

l = LinkedList("x")
l append LinkedList("abc", "asd")

println(l)
// prints 
// LinkedList(x, abc, asd)

Why does the second code snippet works but the first one doesnt? This is on Scala 2.10

为什么第二个代码片段有效而第一个无效?这是在 Scala 2.10 上

回答by drexin

The documentation says If this is empty then it does nothing and returns that. Otherwise, appends that to this.. That is exactly, what you observed. If you really need a mutable list, I would suggest you to use scala.collection.mutable.ListBufferinstead, with it you can do

文档说If this is empty then it does nothing and returns that. Otherwise, appends that to this.。这正是你观察到的。如果你真的需要一个可变列表,我建议你改用scala.collection.mutable.ListBuffer它,你可以这样做

val lb = new ListBuffer[Int]

scala> lb += 1
res14: lb.type = ListBuffer(1)

scala> lb
res15: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1)

scala> lb ++= Seq(1,2,3)
res17: lb.type = ListBuffer(1, 1, 2, 3, 1, 2, 3)

scala> lb
res18: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 1, 2, 3, 1, 2, 3)

回答by ses

As I understand it is related to First/Last (Nil) element in the list (if list is empty Nilis first and last element at the same time).

据我了解,它与列表中的第一个/最后一个 ( Nil) 元素有关(如果列表为空,Nil则同时是第一个和最后一个元素)。

LinkedList (still) follows "primitive charm" strategy. So it does not try to add/append new data to/after Nil, to have possible result like this: {Nil, newElement}. (After all Nilshould be last element)

LinkedList(仍然)遵循“原始魅力”策略。因此,它不会尝试向/之后添加/追加新数据Nil,从而产生如下可能的结果:{Nil, newElement}。(毕竟Nil应该是最后一个元素)

Of course it could check iflist is empty then put addingListto the beginning and Nilto the end. But this would be "too smart", I guess.

当然它可以检查if列表是空的然后放在addingList开头和Nil结尾。但这将是“太聪明了”,我猜。

But, anyway append()returns "expecting" result Like this:

但是,无论如何都会append()返回“预期”结果,如下所示:

val addingList = new LinkedList[String]("a", "b")
val result = emptyList append addingList

result = {"a", "b"}.In this case it returns 'addingList' itself, and/but does not change initial list.

result = {"a", "b"}.在这种情况下,它返回 'addingList' 本身,和/但不更改初始列表。

If we try to assign newElement to the nextref:

如果我们尝试将 newElement 分配给nextref:

   emptyList.next = LinkedList("whatever")

As result we would have emtyList changed like this:

因此,我们会将 emtyList 更改为:

 LinkedList(null, whatever)

I.e. it creates fist element as null, since we have used next()assigning new/next element to it. So it moves Nil to the end, because first element which is null, has next reference to new element we added (addingElelement).

它创建的第一个元素为 null,因为我们已经使用next()分配新/下一个元素给它。所以它将 Nil 移到最后,因为第一个元素为 null,下一个引用我们添加的新元素 ( addingElelement)。

Because

因为

"the "emptyList" is also the "head" link"

““空列表”也是“头”链接”

and head in our case head is Nil, but Nillcan not have next, so it has to create new first element (which is has null value) with next() referece to our new addingElelement.

和 head 在我们的情况下 head is Nil,但Nill不能有 next,因此它必须创建新的第一个元素(具有空值),并使用 next() 引用我们的 new addingElelement

Personally I find it "too much primitive" and not "so much elegant". But it depends, I guess.

我个人认为它“太原始”而不是“太优雅”。但这取决于,我想。

Task oriented story:

任务导向的故事:

For my initial task (why I start thinking about this 'strange' list behaviour [even though it's mutable]) -- I wanted to use mutable list for a class/object called Dictionarywhich would keep Wordsin it (dictionary by default has not any words). And I would have methods like addWord(wod:String)for adding new words. For now my implementation will be changed (I'm not going to use this LinkedList, but rather MutableList. It seems it is more mutablethan previous one):

对于我的初始任务(为什么我开始考虑这个“奇怪”的列表行为[即使它是可变的])——我想使用可变列表作为一个类/对象Dictionary,它会保留Words在其中(字典默认没有任何词) )。而且我会使用类似 a 的方法ddWord(wod:String)来添加新单词。现在我的实现将被改变(我不打算使用 this LinkedList,而是使用MutableList它。它似乎比以前的更易变):

object Dictionary {

  val words = new mutable.MutableList[Word]();

  def addWord(word: Word): Unit = {
    words += word;
  }

}

But possible implementation could be like this:

但可能的实现可能是这样的:

object Dictionary {

  var words = new mutable.LinkedList[Word]();

  def addWord(word: Word): Unit = {

    if (words.isEmpty) {
      words = words append( mutable.LinkedList[Word](word) ) // rely on append result
    } else {
      words append( mutable.LinkedList[Word](word) )
    }

  }

}

But then I have to use varinstead of val, and I should transform every new Wordto LinkedList, and my logic became more complicated.

但是后来我必须使用var而不是val,我应该将每个新单词都转换为LinkedList,我的逻辑变得更加复杂。