arraylist - indexoutofboundsexception

我试图遍历一个数组列表并从中添加或删除项目,这是 Codegym.cc 的一个问题,我不确定解决方案是什么,有什么帮助吗?在我添加最后两个 if 语句之前,我已经让它工作了,但我真的不知道我做错了什么。


public class Cat {

    public static void main(String[] args) throws Exception {

        ArrayList<String> list = new ArrayList<String>();

    list.add("rose"); 

   list.add("measure"); 

    list.add("love"); 

    list.add("lyre"); 

    list.add("wade");

    list.add("bark");         


    list = fix(list);


    for (String s : list) {

        System.out.println(s);

    }

}


public static ArrayList<String> fix(ArrayList<String> list) {

    // write your code here     

        //loop through array

    for (int i = 0; i < list.size();){

        //if contains r and not l -remove it

        if(list.get(i).contains("r") && !list.get(i).contains("l")){


            list.remove(i);

            if(i > 0) {i--;}    

        }

        //if contains l and not r - dub entry 

        if(!list.get(i).contains("r") && list.get(i).contains("l")){


            list.add(i, list.get(i));

            if(i == list.size()-1) {break;}else {i+=2;}

        }

        //contains none - do nothing

        if(!list.get(i).contains("r") && !list.get(i).contains("l")){

            i++;

        }

        //contains both - do nothing

        if(list.get(i).contains("r") && list.get(i).contains("l")){

            i++;

        }


      }




    return list;


 }

}


蝴蝶刀刀
浏览 241回答 2
2回答

郎朗坤

有一点你应该注意。操作数组时,您应该提及到达最后一个元素时要做什么。在 CS 中,数组计数从 0 开始。在长度为 4 的数组中,最后一个元素的索引为 3 而不是 4。问题是如果在没有边界检查的情况下进行增量,则您没有检查最后两个情况。这意味着当您返回第一个 if 语句时,有可能出现异常。另一个问题是 for 循环永远不会结束。这是消除异常的快速解决方案。您必须注意到程序不会停止。您应该指定一些条件以退出 for 循环。注意:你最终会得到一个只有爱这个词的巨大数组。public static ArrayList<String> fix(ArrayList<String> list) {&nbsp; &nbsp; // write your code here&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //loop through array&nbsp; &nbsp; for (int i = 0; i < list.size();){&nbsp; &nbsp; &nbsp; &nbsp; //if contains r and not l -remove it&nbsp; &nbsp; &nbsp; &nbsp; if(list.get(i).contains("r") && !list.get(i).contains("l")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.remove(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i > 0) {i--;}&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //if contains l and not r - dub entry&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(!list.get(i).contains("r") && list.get(i).contains("l")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(i, list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i == list.size()-1) {break;}else {i+=2;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //contains none - do nothing&nbsp; &nbsp; &nbsp; &nbsp; if(!list.get(i).contains("r") && !list.get(i).contains("l")){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //contains both - do nothing&nbsp; &nbsp; &nbsp; &nbsp; if(list.get(i).contains("r") && list.get(i).contains("l")){&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; return list;&nbsp;}

小唯快跑啊

由于您要从列表中删除元素,因此列表的大小会递减,并且您正在运行循环直到列表的大小(实际上已经改变),这就是 OutOfIndexException 的原因:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java