• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collection or array?  RSS feed

 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,



 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:. . . s is defined as new int[2_000_000]; . . .
That is a mistake: you cannot use ints as booleans 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.
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean[] primes = new boolean[2_000_000];

I write wrong.

Resolved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!