简体   繁体   English

ArrayList的java.lang.IndexOutOfBoundsException

[英]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.

相关问题 ArrayList java.lang.IndexOutOfBoundsException - ArrayList java.lang.IndexOutOfBoundsException Java Arraylist 得到 java.lang.IndexOutOfBoundsException? - Java Arraylist got java.lang.IndexOutOfBoundsException? ArrayList上发生java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException occuring on ArrayList java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException 访问已填充的数组列表时,为什么会引发“ java.lang.IndexOutOfBoundsException”? - Why is “java.lang.IndexOutOfBoundsException” thrown when accessing a arraylist that is populated? Arraylist java.lang.IndexOutOfBoundsException:索引 3 超出长度 3 错误的范围 - Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error java.lang.IndexOutOfBoundsException:使用ArrayList时索引:0,大小:0 - java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 while working with ArrayList “线程”main“中的异常 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? - “Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index: 0, Size: 0” with ArrayList? 我需要有关 java.lang.IndexOutOfBoundsException 的帮助 - ArrayList - I need help about java.lang.IndexOutOfBoundsException - ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM