Comparing String elements in an ArrayList alphabetically
<p>In this method I am comparing <code>String</code> elements alphabetically. In other words, if an element in a <code>ArrayList</code> is alphabetically bigger than the minimum <code>String</code> (parameter in the method) and is smaller than the maximum <code>String</code> (also parameter) then this <code>String</code> element should be removed from the <code>ArrayList</code>. </p>

<p>However, after running the method there are no Strings returned, not even those which should stay after the method call. In addition, when the <code>IF</code> condition is not satisfied, it printed out <code>"Error"</code> without returning the list. </p>

<pre><code>import java.util.ArrayList;

public class Main {

    public static ArrayList&lt;String&gt; removeInRange(ArrayList&lt;String&gt; list, String beginning, String ending)
    {

        for (int i = 0; i&lt;list.size(); i++)
        {
            if (list.get(i).compareTo(beginning)&gt; 0 &amp;&amp; list.get(i).compareTo(ending)&lt; 0)
            {
                list.remove(list.get(i));
            }

            else {

                System.out.println("Error");
            }
        } return list;
    }

    public static void main(String[] args) {

        ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();
        list.add("h");
        list.add("e");
        list.add("x");

        removeInRange(list, "a", "k");

    }
}
</code></pre>