为什么会出现ArrayIndexOutOfBoundsException以及如何在Android中避免它?

use*_*237 2 java android indexoutofboundsexception

为什么会ArrayIndexOutOfBoundsException出现以及如何在Android中避免它?

kgi*_*kis 6

当您尝试访问不存在的数组项时,抛出此异常:

String [] myArray = new String[2];

myArray[2] = "something"; // Throws exception
myArray[-1] = "something"; // Throws exception
Run Code Online (Sandbox Code Playgroud)

在访问数组项之前,应检查索引是否为负数且不高于数组长度.