I created a simple method to remove all instances of the desired string from the ArrayList. When checking the method I realized that I was supposed to use .equals() when comparing the value in the ArrayList to the target instead of using "==". Why does "==" work when .equals() should be used to compare the two string values?
public class MyProgram extends ConsoleProgram
{
public void run()
{
ArrayList<String> pokemon = new ArrayList<String>();
pokemon.add("Beedrill");
pokemon.add("Castform");
pokemon.add("Charmander");
pokemon.add("Charmander");
pokemon.add("Ditto");
System.out.println(pokemon);
remove(pokemon, "Charmander");
System.out.println(pokemon);
}
public static void remove(ArrayList<String> list, String target){
for(int i = list.size()-1; i > 0; i--){
if(list.get(i) == target){
list.remove(i);
}
}
}
}
Here's a picture of the working code on sololearn.com.