在这种情况下是否可以避免ArrayIndexOutOfBoundsException?如何避免在这种情况下ArrayIndexOutOfBoundsException?
package com;
public class Hi {
public static void main(String args[]) {
String[] myFirstStringArray = new String[] { "String 1", "String 2",
"String 3" };
if (myFirstStringArray[3] != null) {
System.out.println("Present");
} else {
System.out.println("Not Present");
}
}
}
总是有可能避免一个ArrayIndexOutOfBoundsException,核对'array.length'访问你到底想达到数组元素 –
过吗? –
你的数组将*总是*长度为3,所以索引3 *总是*无效。目前还不清楚你在问什么,但是当你继续使用一个长度为3的数组并且要求索引3时,答案是否定的:你无法避免一个异常。 –