Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果存在,如何从数组中获取最后一个元素。在下面的代码中 num 包含元素数组
var line_ = ln.trim if(!line_.isEmpty) { var num = line_.split(" "); }
只需使用last:
last
var num = line_.split(" ").last;
如果 Array 不为空,则 Last 将起作用。您可能更喜欢 lastOption:
scala> Array.empty[String].lastOption res5: Option[String] = None scala> "ab".toArray.lastOption res6: Option[Char] = Some(b)