简体   繁体   English

ArrayList 索引越界

[英]ArrayList index out of bounds

Why java.lang.IndexOutOfBoundsException is raised in this example, if the size of ArrayList has been predefined?如果已经预定义了 ArrayList 的大小,为什么在此示例中会引发java.lang.IndexOutOfBoundsException How to solve this problem?如何解决这个问题呢?

int size = 2:
ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size);
Integer[] value1 = {1,2,3};
Integer[] value2 = {1,2};
nums.add(1,value1); // java.lang.IndexOutOfBoundsException
nums.add(0,value2);

You cannot put an item in an ArrayList before the other one is set.在设置另一个项目之前,您不能将项目放入 ArrayList。 If you want to do it you'll have to assign null values to the item on place 0 first.如果您想这样做,您必须首先为位置 0 上的项目分配空值。

It will work if you do:如果您这样做,它将起作用:

int size = 2:
ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size);
Integer[] value1 = {1,2,3};
Integer[] value2 = {1,2};
nums.add(0, null);
nums.add(1,value1);
nums.set(0,value2);

edit/ Replaced add by set to replace the null object edit/Replaced add by set 替换空对象

The argument to the ArrayList constructor isn't the size of the list, as your code is assuming; ArrayList构造函数的参数不是列表的大小,正如您的代码所假设的那样; it's the capacity of the underlying storage used by the data structure.它是数据结构使用的底层存储的容量

The capacity will grow as required as you add elements to the list.当您向列表中添加元素时,容量将根据需要增长。 The only reason to specify the initial capacity in the constructor is to pre-allocate a larger capacity if you know you're going to be adding lots of elements.在构造函数中指定初始容量的唯一原因是如果您知道要添加大量元素,则预先分配更大的容量。 This means that the underlying array doesn't havwe to be resized too often as you add them.这意味着底层数组在添加它们时不必经常调整大小。

Regardless of what value you specify in the ArrayList constructor, the size of the list is governed solely by what you put into it, so you can't fetch the item with index of 1 until you've added at least 2 elements.无论您在ArrayList构造函数中指定什么值,列表的大小完全取决于您放入其中的内容,因此在添加至少 2 个元素之前,您无法获取index为 1 的项目。

At the time that you add() to the ArrayList , the length of the list is 0. You cannot add past the current end of the List .在将add()ArrayList ,列表的长度为 0。您不能添加超过List的当前末尾。

I would suggest doing:我建议这样做:

nums.add(value2);
nums.add(value1); 

To get the order you appear to want here.在这里获得您似乎想要的订单。

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList(int ) http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList(int )

What you specify it's the initial capacity, that's just the initial size that the internal array has and that is increased automatically if need be.您指定的是初始容量,这只是内部数组的初始大小,如果需要,它会自动增加。 It has nothing to do with item position , hence your error.它与 item position 无关,因此您的错误。 By the way, the default capacity for ArrayLists is 10, so you are actually making it smaller than default.顺便说一下,ArrayLists 的默认容量是 10,所以你实际上使它小于默认值。

According to the javadoc , When you write根据javadoc ,当你写

ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size);

You're not defining the size of nums , you're defining the initial capacity - in this sense, ArrayLists aren't like built-in arrays ( value1 and value2 in your code).您不是在定义nums大小,而是在定义初始容量- 从这个意义上说,ArrayLists 不像内置数组(代码中的value1value2 )。

You can try it yourself: print the size of nums after each line (code cleaned up to prevent the IndexOutOfBoundsException.您可以自己尝试:在每行之后打印nums的大小(清理代码以防止 IndexOutOfBoundsException。

int size = 2:
ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size);
Integer[] value1 = {1,2,3};
Integer[] value2 = {1,2};
System.out.println(nums.size());
nums.add(value2);
System.out.println(nums.size());
nums.add(value1);
System.out.println(nums.size());

This will print:这将打印:

0
1
2

In order to get the results you expect I would recommend to use base typed arrays like为了获得您期望的结果,我建议使用基本类型的数组,例如

Integer[][] nums = new Integer[size][];

and then进而

Integer[] value1 = {1,2,3};
Integer[] value2 = {1,2};
nums[1] = value1; 
nums[0] = value2;

If you still need an ArrayList just use如果您仍然需要一个 ArrayList 只需使用

new ArrayList<Integer[]>(Arrays.asList(nums));

To get the result you want to have, you have to write为了得到你想要的结果,你必须写

nums.add(0, value1);
nums.add(0, value2);

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

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