posted 8 months ago

Hello,

I have a array that contains boolean values and s[i] is true if i is prim.

so s[0] = false

s[1] = false

s[2] = true

s[3] = true

s[4] = false

s is defined as new int[2_000_000];

I want a way to copy into another array only prime elements.

How can do this issue?

One for is not sufficient because I want that new array index to be zero.

Thanks a lot,

I have a array that contains boolean values and s[i] is true if i is prim.

so s[0] = false

s[1] = false

s[2] = true

s[3] = true

s[4] = false

s is defined as new int[2_000_000];

I want a way to copy into another array only prime elements.

How can do this issue?

One for is not sufficient because I want that new array index to be zero.

Thanks a lot,

OCAJP 7

Campbell Ritchie

Marshal

Posts: 56529

172

posted 8 months ago

- 1

That is a mistake: you cannot useDana Ucaed wrote:. . . s is defined as new int[2_000_000]; . . .

`int`s as

`boolean`s in Java® (shouldn't compile), and it is incorrect to try to store numbers instead. Consider a

`boolean[]`or a List<Boolean> or a BitSet. You will probably find a List<Integer> best for storing prime numbers. Any of the data structures storing true/false can easily be used with a Sieve of Eratosthenes.

It is sorta covered in the JavaRanch Style Guide. |