import java.util.ArrayList; class Lunch { public static ArrayList<String> removeAnts(ArrayList<String> lunchBox) { // Add your code below for (int i = 0; i < lunchBox.size(); i++) { if (lunchBox.get(i) == "ant"){ lunchBox.remove(lunchBox.get(i)); i--; } } /* // Using a while loop int i = 0; while (i < lunchBox.size()) { if (lunchBox.get(i) == "ant"){ lunchBox.remove(lunchBox.get(i)); } else { i++; } } */ return lunchBox; } public static void main(String[] args) { ArrayList<String> lunchContainer = new ArrayList<String>(); lunchContainer.add("apple"); lunchContainer.add("ant"); lunchContainer.add("ant"); lunchContainer.add("sandwich"); lunchContainer.add("ant"); lunchContainer = removeAnts(lunchContainer); System.out.println(lunchContainer); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter