简体   繁体   English

ArrayList 越界异常

[英]ArrayList out of bounds exception

I have the following code:我有以下代码:

ArrayList<Integer> arr = new ArrayList<Integer>(10);
arr.set(0,5);

I am getting an index out of bounds error, and I don't know why.我得到一个索引越界错误,我不知道为什么。 I have declared the ArrayList of size 10. Why do I get this error?我已声明大小为 10 的ArrayList 。为什么会出现此错误?

You declared an ArrayList , that has the initial capacity of 10 elements, but you did not add an element to this list, ie the list is empty.您声明了一个ArrayList ,其初始容量为 10 个元素,但您没有向此列表添加元素,即列表为空。 set will replace an existing element, but since there is no element in the list, the exception is thrown. set将替换现有元素,但由于列表中没有元素,因此抛出异常。 You have to add elements before, using the add method.您必须在使用add方法之前添加元素。

Initial capacity means that the array that the list maintains internally is of size 10 at the beginning. Initial capacity是指列表内部维护的数组一开始大小为10。 While adding more elements to the list, the size of this internal array might change.在向列表中添加更多元素时,此内部数组的大小可能会发生变化。

looking into JDK source code of ArrayList.set(int, E) gives a hint: you need to have at least N elements in your list before you can call set(N-1, E) .查看ArrayList.set(int, E) JDK 源代码给出了一个提示:在调用set(N-1, E)之前,您的列表中至少需要有N元素。

Add your elements via add() method.通过add()方法添加元素。

The initial array contained is specified as "10" the actual number of items in the array is 0.包含的初始数组指定为“10”,数组中的实际项目数为 0。

To add "5" at the first index, just do arr.add(5)要在第一个索引处添加“5”,只需执行 arr.add(5)

The value passed to the ArrayList constructor is the initial capacity of the backing store of the array.传递给 ArrayList 构造函数的值是数组后备存储的初始容量。 When you add elements to a point that exceeds this capacity, internally the ArrayList will allocate a new storage array of a size and copy the items to the new backing store array.当您向超出此容量的点添加元素时,ArrayList 将在内部分配一个新的存储数组,并将项目复制到新的后备存储数组。

From the documentation :文档

Constructs an empty list with the specified initial capacity.构造一个具有指定初始容量的列表。

(emphasis mine) (强调我的)

What you passed in the constructor is just the initial capacity of the array by which the list is backed.您在构造函数中传递的只是支持列表的数组的初始容量。 The list is still empty after construction.该列表在构建后仍然是空的。 Moreover you should consider using a generic list if you want to store ie just Integers..此外,如果您想存储即仅整数,您应该考虑使用通用列表。

You have initialized arraylist with number of items 0 which means you just have 1 element in this array.您已经使用项目数0初始化了 arraylist,这意味着您在该数组中只有1元素。 and later you adding two numbers in this array.然后你在这个数组中添加两个数字。

Use arr.add(0.5) .使用arr.add(0.5) set method will replace the existing element. set方法将替换现有元素。

set(int index, E element) Replaces the element at the specified position in this list with the specified element. set(int index, E element) 用指定的元素替换此列表中指定位置的元素。 U should use add()你应该使用 add()

If you look at the javadoc note that it says:如果您查看javadoc说明,它说:

Replaces the element at the specified position in this list with the specified element.用指定的元素替换此列表中指定位置的元素。

You need an element before you can replace one.您需要一个元素才能替换一个元素。 Try尝试

arr.add(5);

to just add an element.只需添加一个元素。

In the constructor, you have specified an initial capacity.在构造函数中,您已指定初始容量。 However, the size of the list is still 0 because you have not added any elements yet.但是,列表的大小仍然是 0,因为您还没有添加任何元素。

From the documentation of ArrayList.set() :ArrayList.set()的文档中:

IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()). IndexOutOfBoundsException - 如果索引超出范围 (index < 0 || index >= size())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM