[英]java.lang.IndexOutOfBoundsException for ArrayList
I get an java.lang.IndexOutOfBoundsException for this code. 我获得了此代码的java.lang.IndexOutOfBoundsException。
private List<Integer> LoeschenX = new ArrayList<Integer>();
private List<Integer> LoeschenY = new ArrayList<Integer>();
for (int i : LoeschenY) LoeschenX.add(LoeschenY.get(i));
When you do 当你做
for (int i : LoeschenY)
you are looping over the elements of LoeschenY
, not on indexes. 您正在遍历LoeschenY
的元素 ,而不是索引。 You may want to iterate over indexes so you can use get(i)
: 您可能要遍历索引,以便可以使用get(i)
:
for (int i = 0; i < LoeschenY.size(); i++)
LoeschenX.add(LoeschenY.get(i));
Remember that get(index)
will return the value in an specific index. 请记住, get(index)
将返回特定索引中的值。
Edit: You can also try 编辑:您也可以尝试
for (int i : LoeschenY)
LoeschenX.add(i);
since i
takes the values of the elements of LoeschenY
, you will add these values to LoeschenX
. 由于i
采用了LoeschenY
元素的值,因此将这些值添加到LoeschenX
。
You seem to be iterating over the elements in the Y array, but the get
method actually uses the element as an index the way you're doing it. 您似乎正在遍历Y数组中的元素,但是get
方法实际上以执行该操作的方式将元素用作索引 。
Try 尝试
for(int i : LoeschenY)
LoeschenX.add(i);
Or 要么
for(int i = 0; i < LoeschenY.size(); i++)
LoeschenX.add(LoeschenY.get(i));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.