In this method I am comparing String
elements alphabetically. In other words, if an element in a ArrayList
is alphabetically bigger than the minimum String
(parameter in the method) and is smaller than the maximum String
(also parameter) then this String
element should be removed from the ArrayList
.
However, after running the method there are no Strings returned, not even those which should stay after the method call. In addition, when the IF
condition is not satisfied, it printed out "Error"
without returning the list.
import java.util.ArrayList;
public class Main {
public static ArrayList<String> removeInRange(ArrayList<String> list, String beginning, String ending)
{
for (int i = 0; i<list.size(); i++)
{
if (list.get(i).compareTo(beginning)> 0 && list.get(i).compareTo(ending)< 0)
{
list.remove(list.get(i));
}
else {
System.out.println("Error");
}
} return list;
}
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("h");
list.add("e");
list.add("x");
removeInRange(list, "a", "k");
}
}