我会保持简单:我有一个名称的 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 [你好,大家,上午,奋斗,计算机,科学]
我的代码的问题在于,当它开始读取需要删除的新单词时,它不会将单词列表返回到之前的状态。
拉丁的传说
一只斗牛犬
慕的地10843
随时随地看视频慕课网APP
相关分类