了解ArrayList的IndexOutOfBoundsException异常Android中 [英] Understand Arraylist IndexOutOfBoundsException in Android

查看:462
本文介绍了了解ArrayList的IndexOutOfBoundsException异常Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了很多的 IndexOutOfBoundsException异常从任何的ArrayList 我用。大多数正常工作的时间,但有时我得到的ArrayList 这恼人的错误我在我的项目中使用。

的主要原因是始终为

  java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引3,大小为3
 

  java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引0,大小为0
 

帮助我理解这个错误的主要原因是不管有多少的答案我已经搜查,他们并不能完全帮助我。

解决方案
  

java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引3,   尺寸为3

这意味着你的ArrayList其中有3个元素在这里你可以得到每一个元素像0,1,2的位置。而你试图读取不存在到ArrayList中第4个元素。

  

java.util.ArrayList.throwIndexOutOfBoundsException:无效的索引0,   大小为0

这意味着你有一个空的ArrayList,和你想读1个元素。


ArrayIndexOutOfBoundsException异常 - 示例

数组索引出界异常Java异常是由于一个事实,即程序试图访问一个抛出 在一个位置,超出数组的限制,因此将出界的元素。换句话说,该程序正试图 访问一个元素的索引是数组边界之外的。要理解数组边界,让我们考虑如下图:

上面的图片中包含的数组包含7个要素。数组中的每个元素都有自己的索引/位置。在Java中,一个索引总是从 0并结束与阵列的长度-1。例如,该阵列上方由7的元素,因此它的索引从0开始,并与图6(7-1)结束。尝试 访问一个元件与指数大于6小于0以上会造成爪哇抛出一个ArrayIndexOutOfBoundsException


了解更多关于 <一个href="http://voidexception.weebly.com/array-index-out-of-bounds-exception.html">ArrayIndexOutOfBoundsException - 实例,原因及放大器;修复

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project.

The main cause is always either

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

or

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0 

Help me understand the main cause of this error as no matter how many answers I've searched they don't completely help me.

解决方案

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

It means you have ArrayList which having 3 elements where you can get each element like 0,1,2 positions. And you are trying to read 4th element which does not exists into ArrayList.

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0

It means you have a empty ArrayList, and you are trying to read 1st element.


ArrayIndexOutOfBoundsException - Examples

An array-index out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array limit, hence the words "Out of bounds". In other words, the program is trying to access an element at an index that is outside the array bounds. To understand array bounds, let us consider the following diagram:

The picture above contains an array that consists of 7 elements. Each element in the array has its own index/position. In Java, an index always starts with 0 and ends with the length of the array -1. For example, the array above consists of 7 elements, therefore it's indices start from 0 and end with 6 (7-1). Attempting to access an element with an index less than 0 or more than 6 will cause Java to throw an ArrayIndexOutOfBoundsException.


Read more about ArrayIndexOutOfBoundsException - Examples, Causes & Fixes

这篇关于了解ArrayList的IndexOutOfBoundsException异常Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆