追加一个元素在斯卡拉一个列表的末尾 [英] Appending an element to the end of a list in Scala
本文介绍了追加一个元素在斯卡拉一个列表的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这听起来像一个愚蠢的问题,但我在互联网上找到了垃圾桶。
我简直不能添加T类型的元素放入列表列表[T]。
我试着用 myList中:: = myElement
,但似乎它创建一个奇怪的物体和访问到 myList.last
总是返回被列入名单中的第一个元素。
解决方案
列表(1,2,3):+ 4// - >列表[INT] =名单(1,2,3,4)
请注意,此操作有一个为O(n)的复杂性。如果你需要这个操作频繁,或长的列表,请考虑使用其他数据类型(例如ListBuffer)。
It sounds like a stupid question, but all I found on the internet was trash.
I simply 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.
解决方案
List(1,2,3) :+ 4
//--> 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).
这篇关于追加一个元素在斯卡拉一个列表的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文