在 For-Loop 中重新启动 ArrayList

我会保持简单:我有一个名称的 ArrayList,我必须删除包含特定字母的某些单词,但是我无法重新启动 for 循环。这是我得到的:


public static void someRandomFunction(){

    List<String> arrList = new ArrayList<>(Arrays.asList("Hello",

                                                     "Everyone",

                                                     "I'm",

                                                     "Struggling",

                                                     "In",

                                                     "Computer",

                                                     "Science"));


    System.out.println("Start of List: " + wordList + "\n"); 

    System.out.println("\nDrop: \"a\""); 

    someRandomFunction(wordList, "a");

    System.out.println("wordList is now: " + wordList);

}


public static List<String> removeIfContains(List<String> strList, String removeIf){


    List<String> tempList = new ArrayList<>(strList); // creating a copy


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

        if(tempList.get(i).contains(removeIf))

            tempList.remove(i);

    }


//Return will not work because of incompatible types.

}

编译后的代码应该是什么的一个例子:


ArrayList [你好,大家,我,我,挣扎,在,计算机,科学]


删除以“A”开头的单词:


新的 ArrayList [你好,大家,我,正在奋斗,在,计算机,科学]


删除以“I”开头的单词:


新的 ArrayList [你好,大家,上午,奋斗,计算机,科学]


我的代码的问题在于,当它开始读取需要删除的新单词时,它不会将单词列表返回到之前的状态。


拉丁的传说
浏览 228回答 2
2回答

一只斗牛犬

如果您只想删除ArrayList以某个字母开头的每个元素,您可以使用以下removeIf()方法:删除此集合中满足给定谓词的所有元素。wrodList.removeIf(e&nbsp;->&nbsp;e.contains(thisLetter));(需要 Java 8+)听起来您希望在每次删除元素后重置列表。为此,您可以创建一个副本ArrayList进行检查,然后在每次之后将其设置回原始副本:List<String>&nbsp;copy&nbsp;=&nbsp;new&nbsp;ArrayList<>(wordList);&nbsp;//Creates&nbsp;a&nbsp;copy&nbsp;of&nbsp;wordList

慕的地10843

我相信这就是你正在寻找的。我不确定你是想要一个实例还是静态方法。我相信您的问题是您没有创建副本。我记下了我在哪里创建副本。祝你在 CS 中好运......我们都曾一度陷入困境。public static void someRandomFunction(){&nbsp; &nbsp; List<String> arrList = new ArrayList<>(Arrays.asList("Hello",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Everyone",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"I'm",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Struggling",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"In",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Computer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"Science"));&nbsp; &nbsp; System.out.println(removeIfContains(arrList, "H")); // calling the function and passing the list and what&nbsp; &nbsp; System.out.println(removeIfContains(arrList, "I")); // I want to remove from the list}public static List<String> removeIfContains(List<String> strList, String removeIf){&nbsp; &nbsp; List<String> tempList = new ArrayList<>(strList); // creating a copy&nbsp; &nbsp; for(int i = 0; i < tempList.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if(tempList.get(i).contains(removeIf))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempList.remove(i);&nbsp; &nbsp; }&nbsp; &nbsp; return tempList; // returning the copy}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java