I need to use the method "remove" that accepts an ArrayList of Strings and a target String as two parameters, and searches and deletes all matched Strings from the ArrayList. For example, calling remove on the ArrayList [“Go”, “Go”, “Go”] and the target String “Go”, would result in an empty ArrayList.
public class Remove {
public static void main (String [] args) {
ArrayList<String> x = new ArrayList<>();
x.add("Go");
x.add("Go");
x.add("Go");
System.out.println(x);
remove("Go");
System.out.println(x);
}
public static void remove(String t) {
ArrayList<String> x = new ArrayList<>();
for(int i = 0; i < x.size(); i++) {
if(x.get(i).equals(t)) {
x.remove(i);
i--;
}
}
}
}
xis empty. Did you mean to pass thexfrom themainmethod as a parameter?