我正在尝试删除以 r 和 g 开头的列表元素
一个条件可以很好地删除从 r 开始的元素,但下一个不能在 if 循环中帮助我出错的地方。谢谢..!!
我的代码是
import java.util.Arrays;
import java.util.LinkedList;
public class EasyRemovingList {
void removeList(){
String[] inpArray={"red","green","blue","ivory"};
LinkedList<String> upList = new LinkedList<String>(Arrays.asList(inpArray));
System.out.println("Actual "+upList+" "+upList.size());
//System.out.println(upList.get(3));
for(int i = 0;i<upList.size();i++){
//System.out.println(upList.get(i));
if(upList.get(i).startsWith("r")||upList.get(i).startsWith("g")){
upList.remove(i);
System.out.println("Updated "+upList);
}
}
//System.out.println(upList);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
EasyRemovingList er = new EasyRemovingList();
er.removeList();
}
}
输出是
Actual [red, green, blue, ivory] 4
Updated [green, blue, ivory]
预期输出是
Actual [red, green, blue, ivory] 4
Updated [blue, ivory]
胡子哥哥
相关分类