为什么ArrayIndexOutOfBoundsException异常发生,以及如何避免它的Andr​​oid?

问题描述:

为什么 ArrayIndexOutOfBoundsException异常发生,以及如何避免它在Android的?

Why does ArrayIndexOutOfBoundsException occur and how to avoid it in Android?

当您试图访问一个不存在的数组项,抛出此异常:

This exception is thrown when you try to access an array item that doesn't exist:

String [] myArray = new String[2];

myArray[2] = "something"; // Throws exception
myArray[-1] = "something"; // Throws exception

您应该检查你的指数是不是消极的,不高于该数组长度访问数组项目之前。

You should check that your index is not negative and not higher than the array length before accessing an array item.