list 选择列表的最后一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5104087/
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
Choosing the last element of a list
提问by dondog
scala> last(List(1, 1, 2, 3, 5, 8))
res0: Int = 8
for having a result above, I wrote this code:
为了得到上面的结果,我写了这段代码:
val yum = args(0).toInt
val thrill:
def last(a: List[Int]): List[Int] = {
println(last(List(args(0).toInt).last)
}
What is the problem with this code?
这段代码有什么问题?
回答by michael.kebe
You can use last
, which returns the last element or throws a NoSuchElementException
, if the list is empty.
您可以使用last
, 返回最后一个元素或抛出 a NoSuchElementException
,如果列表为空。
scala> List(1, 2, 3).last
res0: Int = 3
If you do not know if the list is empty or not, you may consider using lastOption
, which returns an Option
.
如果您不知道列表是否为空,您可以考虑使用lastOption
,它返回一个Option
。
scala> List().lastOption
res1: Option[Nothing] = None
scala> List(1, 2, 3).lastOption
res2: Option[Int] = Some(3)
Your question is about List
, but using last
on a infinite collection (e.g. Stream.from(0)
) can be dangerous and may result in an infinite loop.
您的问题是关于List
,但last
在无限集合(例如Stream.from(0)
)上使用可能很危险,并可能导致无限循环。
回答by Jus12
Another version without using last
(for whatever reason you might need it).
另一个不使用的版本last
(无论出于何种原因您可能需要它)。
def last(L:List[Int]) = L(L.size-1)
回答by Pere Villega
You should better do:
你最好这样做:
val a = List(1,2,3) //your list
val last = a.reverse.head
Cleaner and less error-prone :)
更干净,更不容易出错:)
回答by Ravi
The recursive function last should following 2 properties. Your lastfunction doesn't have any of them.
递归函数最后应该遵循 2 个属性。你的最后一个函数没有它们中的任何一个。
Requirement #1. An exit condition that does not call recursive function further.
Requirement #2. A recursive call that reduces the elements that we began with.
要求#1。不进一步调用递归函数的退出条件。
要求#2。减少我们开始使用的元素的递归调用。
Here are the problems I see with other solutions.
以下是我在其他解决方案中看到的问题。
- Using built in function last might not be an option in interview questions.
- Reversing and head takes additional operations, which the interviewer might ask to reduce.
- What if this is a custom linked list without the size member?
- 使用内置函数 last 可能不是面试问题的选项。
- 反转和头部需要额外的操作,面试官可能会要求减少这些操作。
- 如果这是一个没有大小成员的自定义链表怎么办?
I will change it to as below.
我将其更改为如下所示。
def last(a: List[Int]): Int = a match {
//The below condition defines an end condition where further recursive calls will not be made. requirement #1
case x::Nil => x
//The below condition reduces the data - requirement#2 for a recursive function.
case x:: xs => last(xs)
}
last(List(1,2,3))
Result
结果
res0: Int = 3
回答by Marton Tatai
Albiet this is a very old question, it might come handy that the performance impact of head and last operations seems to be laid out here http://docs.scala-lang.org/overviews/collections/performance-characteristics.html.
尽管这是一个非常古老的问题,但 head 和 last 操作的性能影响似乎在这里列出http://docs.scala-lang.org/overviews/collections/performance-characteristics.html可能会派上用场。
回答by Sohum Sachdev
This is where the beauty of Scala comes to play!
这就是 Scala 之美发挥作用的地方!
val l: List[Int] = List(1,2,3,4)
val lastOption: Option[Int] = l.lastOption
By doing this, you will get an Option
of Int
. Read more about Scala's Option's here.
通过这样做,你会得到一个Option
的Int
。在此处阅读有关 Scala 选项的更多信息。
Then finally, you can handle the None
case as you please:
最后,你可以随心所欲地处理这个None
案例:
val last: Int = lastOption.getOrElse(0) //or however else you want to handle the case of an empty list