1

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);
}
}
11
  • 5
    java has no integer. The primitive is int, the object is Integer. Commented May 10, 2013 at 9:16
  • Also, "getting some errors" is no help to us. Please provide the errors. Commented May 10, 2013 at 9:16
  • Can you elaborate on "I'm getting some errors"? Commented May 10, 2013 at 9:17
  • Array deques have no capacity restrictions; they grow as necessary to support usage Commented May 10, 2013 at 9:17
  • 3
    I think the whole implementation of finding prime numbers is not correct! Commented May 10, 2013 at 9:21

1 Answer 1

3

There's no thing in Java like integer. The proper one is Integer.

import java.util.ArrayDeque;
public class MyClass {
  public static void main(String args[]) {

    int x = 2;
    Deque<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);
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't this be more correct: Deque<Integer> primes = new ArrayDeque<>(8); ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.