I'm trying to store a list of primes using java and came across ArrayDeque. I'm not sure if this is the correct occasion to use it, but since I don't know the number of primes I need the capacity to grow.
The code is designed to go through numbers 2 to 1000 and test if they are a prime or not.
I'm getting some errors. I am pretty new to this so if anybody could guide me in the right direction that would be great. Is using a Array with a large pre-set capacity a better way of doing things?
Many thanks, Behzad
import java.util.ArrayDeque;
import java.util.Deque;
public class Maths {
public static void main (String[] arg) {
int x = 2;
ArrayDeque<integer> primes = new ArrayDeque<integer>(8);
for(int count = 2; count<1000; count++) {
if (x%count == 0) {
System.out.println("Number is not prime"); // If it isn't a prime, it moves onto the next number.
x = x + 1;
count = 2;
}
else if (x >1000) {
break;
}
else if (count == x - 1) {
System.out.println( x + " is a prime"); //This possibility singles out prime numbers
primes.add(x);
x = x + 1; // Need to find a way to add them to memory.
count = 2;
}
}
System.out.println("Searchfinished");
System.out.println(primes);
}
}
integer. The primitive isint, the object isInteger.Array deques have no capacity restrictions; they grow as necessary to support usage