Java ArrayList 索引越界异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21507552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList index out of bounds exception
提问by Sathish
Recently while using arrayList I found a weird issue. I want to keep 2 elements in the arraylist and would like to keep object1 in element 0 and object2 in element1.
最近在使用 arrayList 时,我发现了一个奇怪的问题。我想在数组列表中保留 2 个元素,并希望在元素 0 中保留 object1,在 element1 中保留 object2。
I am deciding this in a loop.
我在循环中决定这个。
When it happens to add the first element its throwing indexOutofBounds. As per java doc as the index is greater than size its doing this.
当它碰巧添加第一个元素时,它会抛出 indexOutofBounds。根据java doc,因为索引大于大小,所以这样做。
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(1,"SECONDITEM"); // throwing due to size is not set
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Then I tried to setup other items as placeholder in 0,1 element and tried to do same
然后我尝试将其他项目设置为 0,1 元素中的占位符并尝试做同样的事情
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(0,"x");
list.add(1,"y");
list.add(1,"SECONDITEM");
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Output:
Position 0 :FIRSTITEM
Position 1 :x
Position 2 :SECONDITEM
Position 3 :y
How to get over this issue. One way I found is to remove the element in the index and add the element. Is there a better option.
如何克服这个问题。我发现的一种方法是删除索引中的元素并添加元素。有没有更好的选择。
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(0,"x");
list.add(1,"y");
list.remove(1);
list.add(1,"SECONDITEM");
list.remove(0);
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Position 0 :FIRSTITEM
Position 1 :SECONDITEM
回答by Deepak Sharma
.add() methodinsert the element at the given position and shift the other element accordingly
.add() 方法在给定位置插入元素并相应地移动另一个元素
to overwrite the value at particular index you have to use the .set(position, value)
要覆盖特定索引处的值,您必须使用.set(position, value)
try the set method ..
尝试设置方法..
list.add(0,"x");
list.add(1,"y");
list.set(1,"SECONDITEM");
list.set(0,"FIRSTITEM");
now it will return the only two values.
现在它将返回仅有的两个值。
回答by Alvin Bunk
When you call in this order:
当您按此顺序调用时:
ArrayList<String> list = new ArrayList<String>();
list.add(1,"SECONDITEM"); // throwing due to size is not set
You are trying to add to position "1", but nothing has been added to the list yet. I believe that is why you get the error. Instead you probably want to use just:
您正在尝试添加到位置“1”,但尚未向列表添加任何内容。我相信这就是你得到错误的原因。相反,您可能只想使用:
ArrayList<String> list = new ArrayList<String>();
list.add("FIRSTITEM");
Since it's an empty list. You can also use the index, but you'll have to increment each time. Makes sense?
因为它是一个空列表。您也可以使用索引,但每次都必须递增。说得通?
回答by Brian Roach
The issue is ... you're trying to use a List
in a way it wasn't intended to be used. As you've found out, you can't set or add to an index that is outside the range of the current size.
问题是......您正试图以一种List
不打算使用的方式使用 a 。正如您所发现的,您无法设置或添加超出当前大小范围的索引。
If you know in advance the size you need and it's a fixed size, using a String[]
is the better choice.
如果您事先知道您需要的尺寸并且它是固定尺寸,则使用 aString[]
是更好的选择。
If you really want/need a dynamic structure that allows random access sets regardless of the current size, write your own method to fill the list when needed:
如果您真的想要/需要一个允许随机访问集而不管当前大小的动态结构,请在需要时编写自己的方法来填充列表:
public static <T> void fillAndSet(int index, T object, List<T> list)
{
if (index > (list.size() - 1))
{
for (int i = list.size(); i < index; i++)
{
list.add(null);
}
list.add(object);
}
else
{
list.set(index, object);
}
}