如何增加arraylist的大小?

时间:2022-05-20 16:51:47

I'm trying to increase the size of an array list but it doesnt seem to want to work.

我正在尝试增加数组的大小,但它似乎不想工作。

I am doing this:

我这样做:

    final int QUEUE_CAPACITY = 27;
    ArrayList adjacencyList = new ArrayList<Integer>(QUEUE_CAPACITY);

But when I try to add something with the add that has two paramters, (index,value) I get this error:

但是当我尝试添加有两个参数的加法时,(index,value)我得到这个错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

4 个解决方案

#1


3  

You have established the initial capacity of the ArrayList, but there are still no items in the list. You can't insert an item before index 1 if it doesn't exist yet.

您已经确定了ArrayList的初始容量,但是列表中仍然没有项目。如果索引1还不存在,就不能在索引1之前插入项。

You must use the one-argument add method to append to the end of the list, or supply 0 as the index in the two-argument add method, so that there is something in the list.

您必须使用one-argument add方法将其追加到列表的末尾,或者在双参数add方法中以0作为索引,这样就有了列表中的某些内容。

#2


2  

Well, If you really want to do this.

如果你真想这么做的话。

You need to Intialise the Array with 'Integers' like '0' or so and then instead of add() method use the set(int index, E element) method which Replaces the element at the specified position in this list with the specified element.

您需要将数组以“0”之类的“整数”进行初始化,而不是添加()方法使用set(int index, E元素)方法,该方法用指定的元素替换该列表中指定位置的元素。

Any index at which you see '0' is equivalent to empty.

任何你看到“0”的指数都等于空的。

#3


0  

Are you calling the equivalent of adjacencyList.add(1, 1)? If so, you're trying to add to an empty list at index 1, which doesn't exist. Just call adjacencyList.add(<value>).

你把它叫做邻接表吗?add(1,1)?如果是这样,那么您试图在索引1中添加一个空列表,该列表不存在。就叫adjacencyList.add( <价值> )。

Also, include the generic type at initialization:

另外,在初始化时包含泛型类型:

ArrayList<Integer> adjacencyList = new ArrayList<Integer>(QUEUE_CAPACITY);

PS. You don't usually need to adjust the initial capacity of an ArrayList unless you know the list will rarely end up being small.

你通常不需要调整ArrayList的初始容量,除非你知道这个列表很少会变成小的。

#4


0  

That's not how it works.

事情不是这样的。

If you're going to insert something at a certain position, you need to have elements stored up to the point in the list already.

如果要在某个位置插入某个东西,则需要将元素存储到列表中的点。

#1


3  

You have established the initial capacity of the ArrayList, but there are still no items in the list. You can't insert an item before index 1 if it doesn't exist yet.

您已经确定了ArrayList的初始容量,但是列表中仍然没有项目。如果索引1还不存在,就不能在索引1之前插入项。

You must use the one-argument add method to append to the end of the list, or supply 0 as the index in the two-argument add method, so that there is something in the list.

您必须使用one-argument add方法将其追加到列表的末尾,或者在双参数add方法中以0作为索引,这样就有了列表中的某些内容。

#2


2  

Well, If you really want to do this.

如果你真想这么做的话。

You need to Intialise the Array with 'Integers' like '0' or so and then instead of add() method use the set(int index, E element) method which Replaces the element at the specified position in this list with the specified element.

您需要将数组以“0”之类的“整数”进行初始化,而不是添加()方法使用set(int index, E元素)方法,该方法用指定的元素替换该列表中指定位置的元素。

Any index at which you see '0' is equivalent to empty.

任何你看到“0”的指数都等于空的。

#3


0  

Are you calling the equivalent of adjacencyList.add(1, 1)? If so, you're trying to add to an empty list at index 1, which doesn't exist. Just call adjacencyList.add(<value>).

你把它叫做邻接表吗?add(1,1)?如果是这样,那么您试图在索引1中添加一个空列表,该列表不存在。就叫adjacencyList.add( <价值> )。

Also, include the generic type at initialization:

另外,在初始化时包含泛型类型:

ArrayList<Integer> adjacencyList = new ArrayList<Integer>(QUEUE_CAPACITY);

PS. You don't usually need to adjust the initial capacity of an ArrayList unless you know the list will rarely end up being small.

你通常不需要调整ArrayList的初始容量,除非你知道这个列表很少会变成小的。

#4


0  

That's not how it works.

事情不是这样的。

If you're going to insert something at a certain position, you need to have elements stored up to the point in the list already.

如果要在某个位置插入某个东西,则需要将元素存储到列表中的点。