public static void main(String[] args) { List<String> platformList = new ArrayList<>(); platformList.add("博客园"); platformList.add("CSDN"); platformList.add("掘金"); for (String platform : platformList) { if (platform.equals("博客园")) { platformList.remove(platform); } } System.out.println(platformList); }
platformList.remove(platform);
public static void main(String[] args) { List<String> platformList = new ArrayList<>(); platformList.add("博客园"); platformList.add("CSDN"); platformList.add("掘金"); Iterator<String> iterator = platformList.iterator(); while (iterator.hasNext()) { String platform = iterator.next(); if (platform.equals("博客园")) { iterator.remove(); } } System.out.println(platformList); }
[CSDN, 掘金]
public static void main(String[] args) { List<String> platformList = new ArrayList<>(); platformList.add("博客园"); platformList.add("CSDN"); platformList.add("掘金"); for (int i = 0; i < platformList.size(); i ) { String item = platformList.get(i); if (item.equals("博客园")) { platformList.remove(i); i = i - 1; } } System.out.println(platformList); }
i = i - 1;
public static void main(String[] args) { List<String> platformList = new ArrayList<>(); platformList.add("博客园"); platformList.add("CSDN"); platformList.add("掘金"); for (int i = platformList.size() - 1; i >= 0; i--) { String item = platformList.get(i); if (item.equals("掘金")) { platformList.remove(i); } } System.out.println(platformList); }