简体   繁体   English

Arraylist java.lang.IndexOutOfBoundsException:索引 3 超出长度 3 错误的范围

[英]Arraylist java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3 Error

I created a method which takes an Arraylist of string and an integer.我创建了一个方法,它采用Arraylist字符串和 integer。 It's going to remove all string whose length is less than the given integer.它将删除所有长度小于给定 integer 的字符串。

For example:例如:

Arraylist = ["abcde", "aabb", "aaabbb", "abc", "ab"] Arraylist = [“abcde”,“aabb”,“aaabbb”,“abc”,“ab”]

integer = 4 integer = 4

So the new Arraylist should be: ["abcde", "aabb", "aaabbb"]所以新的Arraylist应该是:["abcde", "aabb", "aaabbb"]

But I'm getting this error message:但我收到此错误消息:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引 3 超出长度 3 的范围

Here is my code:这是我的代码:

public static void main(String[] args){
        ArrayList<String> newArrayList = new ArrayList<>();
        newArrayList.add("string1");
        newArrayList.add("string2");
        newArrayList.add("rem");
        newArrayList.add("dontremove");
        removeElement(newArrayList, 4); // new arraylist must be = [string1, string2, dontremove]
    }


    public static void removeElement(ArrayList<String> arraylist, int inputLen){
        int arrayLen = arraylist.size();
        for(int i=0; i<arrayLen; i++){
            if(arraylist.get(i).length() < inputLen){
                arraylist.remove(i);
                i--;
            }
        }
        System.out.println("New Arraylist: " + arraylist);
    }

What's wrong with this code?这段代码有什么问题?

You're looping from 0 to the size of the array which is 4. Inside the loop you're removing items so the size of the array becomes less than 4, so you get this exception.您正在从 0 循环到数组的大小,即 4。在循环内,您正在删除项目,因此数组的大小变得小于 4,因此您会遇到此异常。 Try looping like this:尝试像这样循环:

Iterator<String> iterator = arraylist.iterator();

while (iterator.hasNext()) {
    String word = iterator.next();
    if (word.length() < inputLen) {
        iterator.remove();
    }
}

You're modifying the list while iterating over its indexes.您正在修改列表,同时迭代其索引。 Once you remove the first item, the list will be shorter than you expect, and you'll get this error once you reach the index of the original last element.删除第一项后,列表将比您预期的要短,一旦到达原始最后一个元素的索引,您将收到此错误。 Luckily, the removeIf method can do the heavy lifting for you:幸运的是, removeIf方法可以为您完成繁重的工作:

public static void removeElement(List<String> arraylist, int inputLen) {
    arraylist.removeIf(s -> s.length() < inputLen);
    System.out.println("New Arraylist: " + arraylist);
}
int arrayLen = arraylist.size();

The problem is that your index has a value of 4.问题是您的索引值为 4。

But then as soon as you remove an item from the list you now only have 3 entries.但是,一旦您从列表中删除一个项目,您现在只有 3 个条目。

So in your loop when you try to access the the 4th entry you will now get an IndexOutOfBounds error.因此,在您的循环中,当您尝试访问第 4 个条目时,您现在将收到IndexOutOfBounds错误。

The solution is to start at the end of the ArrayList and count down to 0.解决方法是从 ArrayList 的末尾开始,倒数到 0。

For (int i = arrayLen - 1; i >= 0; i--)

and inside the loop you don't need the:在循环内你不需要:

i--;

The length of the array should change when you remove elements.删除元素时,数组的长度应该会改变。 The arrayLen variable stores the length of the array, but doesn't change when the array's length shrinks. arrayLen变量存储数组的长度,但在数组长度缩小时不会改变。 To change it, you should be able to just replace arrayLen with arrayList.size() , which will change when your remove elements要更改它,您应该能够将arrayLen替换为arrayList.size() ,当您删除元素时它会改变

暂无
暂无

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

相关问题 如何修复ArrayList java.lang.IndexOutOfBoundsException:长度为2的索引20越界 - How to fix ArrayList java.lang.IndexOutOfBoundsException: Index 20 out-of-bounds for length 2 java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围 - java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 收到此错误 java.lang.IndexOutOfBoundsException:索引 487 超出长度 487 的范围 - Getting this error java.lang.IndexOutOfBoundsException: Index 487 out of bounds for length 487 为什么我会收到“线程中的异常”main“java.lang.IndexOutOfBoundsException: IndexOutOfBoundsException: Index 0 out of bounds for length 0” - why do I get an “Exception in thread ”main“ java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 ” 如何解决线程“AWT-EventQueue-0”中的异常 java.lang.IndexOutOfBoundsException:索引 0 超出长度 0 的范围 - how to solve Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 java.lang.IndexOutOfBoundsException: index=3 越界 (limit=6, nb=4) - java.lang.IndexOutOfBoundsException: index=3 out of bounds (limit=6, nb=4) java.lang.IndexOutOfBoundsException:索引:1,大小:1 错误? - java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 error? ArrayList的java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException for ArrayList ArrayList java.lang.IndexOutOfBoundsException - ArrayList java.lang.IndexOutOfBoundsException java.lang.IndexOutOfBoundsException:使用ArrayList时索引:0,大小:0 - java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 while working with ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM