[英]Index out of bounds exception with indexof
def toLowerCase(str: String): String = {
val lowerCase = "abcdefghijklmnopqrstuvwxyz".split("")
val upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
var returnStr = ""
str.split("").foreach(c => if (lowerCase.contains(c)) returnStr += c
else returnStr += lowerCase(upperCase.indexOf(c)))
returnStr
}
此代码段导致
java.lang.ArrayIndexOutOfBoundsException: -1
不确定在这种情况下会导致索引为-1的原因
indexOf
可以返回-1
( str
不仅可以包含拉丁字母)。
/** Finds index of first occurrence of some value in this $coll.
*
* @param elem the element value to search for.
* @tparam B the type of the element `elem`.
* @return the index of the first element of this $coll that is equal (as determined by `==`)
* to `elem`, or `-1`, if none exists.
*
* @usecase def indexOf(elem: A): Int
* @inheritdoc
*
* $mayNotTerminateInf
*
*/
def indexOf[B >: A](elem: B): Int = indexOf(elem, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.