Reputation: 665
How to get the last element from array if present. In below code num contains array of elements
var line_ = ln.trim
if(!line_.isEmpty) {
var num = line_.split(" ");
}
Upvotes: 32
Views: 43787
Reputation: 1143
Last will work if the Array is not empty. You might prefer lastOption:
scala> Array.empty[String].lastOption
res5: Option[String] = None
scala> "ab".toArray.lastOption
res6: Option[Char] = Some(b)
Upvotes: 17