为什么queue.get()
会返回一个空列表?
class MyQueue{
var queue=List[Int](3,5,7)
def get(){
this.queue.head
}
}
object QueueOperator {
def main(args: Array[String]) {
val queue=new MyQueue
println(queue.get())
}
}
如何获取第一个元素?
def proc(...) { ... }
,转而采用完整形式def proc(...): Unit = { ... }
的讨论正在进行中。请参见https://groups.google.com/forum/?fromgroups=#!topic/scala-debate/8G3WgfZNj9E。 - ghiktake 1
而不是 head
更加优秀,因为它不会在空列表上引发异常,并且会返回一个空列表。headOption
更好,因为它不使用任何魔法常量(1),并且返回的类型(Option[T]
)在编译期就可以被静态保证最大容量为1。 - Robin Green
get
方法存在问题。 - Chris Martin