拆分没有索引超出限制的异常

Joh*_*ohn 2 scala

有没有简单的方法通过避免异常的索引绑定来安全地拆分?

例如:

"1-2".split("-")(1)= 2
"1".split("-")(1) = null
Run Code Online (Sandbox Code Playgroud)

bot*_*aio 9

此功能已在实施中实现Array.如果index超出范围,则返回liftmethod None,否则返回Some(value)

val array = Array(3, 5, 1)
array.lift(5) // None
array.lift(0) // Some(3)
Run Code Online (Sandbox Code Playgroud)