为了账号安全,请及时绑定邮箱和手机立即绑定

在 For-Loop 中重新启动 ArrayList

在 For-Loop 中重新启动 ArrayList

拉丁的传说 2021-12-10 16:57:50
我会保持简单:我有一个名称的 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 [你好,大家,上午,奋斗,计算机,科学]我的代码的问题在于,当它开始读取需要删除的新单词时,它不会将单词列表返回到之前的状态。
查看完整描述

2 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

如果您只想删除ArrayList以某个字母开头的每个元素,您可以使用以下removeIf()方法:

删除此集合中满足给定谓词的所有元素。

wrodList.removeIf(e -> e.contains(thisLetter));

(需要 Java 8+)

听起来您希望在每次删除元素后重置列表。为此,您可以创建一个副本ArrayList进行检查,然后在每次之后将其设置回原始副本:

List<String> copy = new ArrayList<>(wordList); //Creates a copy of wordList


查看完整回答
反对 回复 2021-12-10
?
慕的地10843

TA贡献1785条经验 获得超8个赞

我相信这就是你正在寻找的。我不确定你是想要一个实例还是静态方法。我相信您的问题是您没有创建副本。我记下了我在哪里创建副本。祝你在 CS 中好运......我们都曾一度陷入困境。


public static void someRandomFunction(){

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

                                                         "Everyone",

                                                         "I'm",

                                                         "Struggling",

                                                         "In",

                                                         "Computer",

                                                         "Science"));


    System.out.println(removeIfContains(arrList, "H")); // calling the function and passing the list and what

    System.out.println(removeIfContains(arrList, "I")); // I want to remove from the list

}


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 tempList; // returning the copy

}


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 184 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号