【问题标题】:Restart ArrayList within For-Loop在 For-Loop 中重启 ArrayList
【发布时间】:2019-04-01 07:16:30
【问题描述】:

我会保持简单: 我有一个名称的 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 [大家好,我是 Struggling,In,Computer,Science]

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

新的 ArrayList [Hello, Everyone, Am, Struggling, Computer, Science]

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

【问题讨论】:

  • 如果您需要保留原始状态,请在进行更改之前复制列表。 List&lt;String&gt; wordListCopy = new ArrayList&lt;&gt;(wordList)
  • 您需要对同一个列表进行更改还是可以使用新列表?
  • 否,因此该列表已经以特定方式设置。我没有更改 ArrayList。我只希望它适用于我必须删除以特定字母开头的单词的每个实例。
  • 为什么不创建一个新列表,其中包含留在字母中的单词并使用它?

标签: java arrays for-loop arraylist while-loop


【解决方案1】:

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

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

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

(需要 Java 8+)

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

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

【讨论】:

  • 说明如果包含则删除;不是开头。好办法。这种方法需要 Java 8
  • 嘿!谢谢!但是, wordList.removeIf() 不适用于我的情况。另外,我有 ArrayList 副本,我怎样才能以某种方式使用它来重新启动 for-Loop?
  • @KristyCompany 在副本上使用removeIf()
  • removeIf () 不适用于我正在使用的程序。当我编译它时,它无法识别。
  • 您使用的版本可能低于 Java 8 @KristyCompany
【解决方案2】:

我相信这就是您正在寻找的。我不确定您是否需要实例或静态方法。我相信您的问题是您没有创建副本。我注意到我在哪里创建副本。祝你在 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
}

【讨论】:

  • Kristy,您想接受 List 而不是 ArrayList。这就是返回类型不起作用的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多