编写一个 removeEvenLength 方法,该方法将一个字符串 ArrayList 作为参数,并从列表中删除所有长度为偶数的字符串。
public void removeEvenLength(ArrayList <String> stringList){
for(int i=0;i<stringList.size();i++){
String word=stringList.get(i);
if(word.length()%2==0){//even
stringList.remove(word);//if it is even,test from the first word then continue looping
}
}
}
当我尝试传入 ["This", "is", "a", "test"] 时,它应该返回 a。但它却给我 is,a。它有什么问题吗?
最佳答案
提示:您正在迭代期间从列表中删除当前项目。考虑一下这可能会对后续迭代产生什么影响。
关于java - 删除偶数长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853924/