Printing prime numbers with ArrayList
<p>Can anyone tell me what's wrong with the code below?
I wanted to print all prime numbers smaller then 41 but it prints also 15, 21 which are not prime.</p>

<pre><code>import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int x = 41;
        List&lt;Integer&gt; lista = new ArrayList&lt;Integer&gt;();

        for (int i = 1; i&lt;=x; i++){

            lista.add(i);
        }
        System.out.println(lista);



        for (int i = 0; i&lt;lista.size()-1; i++){
        if (!isPrime(lista.get(i))){
            lista.remove(lista.get(i));
            }
        }
        System.out.println(lista);
    }

    public static boolean isPrime(int x){
        boolean itIs = true;
        for (int i = 2; i &lt; x; i++){
            if(x%i == 0){
                itIs = false;  
            }
        }
        return itIs;
    }
}
</code></pre>