fork download
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4.  
  5. class Hello {
  6. public static int[] primesTo(int limit) {
  7. boolean[] sieve = new boolean[limit + 1];
  8. Arrays.fill(sieve, true);
  9.  
  10. for(int p = 3; p <= Math.sqrt(limit); p += 2)
  11. if(sieve[p])
  12. for(int i = 3; i <= limit / p; i += 2)
  13. sieve[p * i] = false;
  14.  
  15. List<Integer> primes = new ArrayList<Integer>(limit / 2);
  16. primes.add(2);
  17. for(int i = 3; i <= limit; i += 2)
  18. if(sieve[i])
  19. primes.add(i);
  20.  
  21. return (int[])primes.toArray(new int[primes.size()]);
  22. }
  23. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:21: error: no suitable method found for toArray(int[])
    return (int[])primes.toArray(new int[primes.size()]);
                        ^
    method List.<T>toArray(T[]) is not applicable
      (inferred type does not conform to declared bound(s)
        inferred: int
        bound(s): Object)
    method List.toArray() is not applicable
      (actual and formal argument lists differ in length)
  where T is a type-variable:
    T extends Object declared in method <T>toArray(T[])
1 error
stdout
Standard output is empty