• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning values into a boolean array from a method  RSS feed

 
Nunya business
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I'm having a problem with returning values into the boolean array sieve. (I haven't included all of my main method, to keep the post short.) Basically i can get everything to work except changing the values within the array using the method processSieve. What can i do to make this work?


 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nunya,

first of all: your processSieve method should return a boolean, according to its signature, however you return the input array. That is wrong of course.

Also, when the processSieve is invoked, the parameter 'arr' already references your original 'sieve' array. So, every change you make to that array in 'processSieve' gets reflected in the array 'sieve' as well. So, in fact you do not need to return anything at all.

Ohter remark: some values in your 'sieve' array are supposed to be true. So, you should initialize this 'sieve' array with 'true 'for every element. In the 'Arrays' class you will find a very handy method to do this.

And lastly: in the processSieve, do you think that second 'for' loop is necessary?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note: l (el) is a terrible choice for a variable name.  To illustrate, which of these is correct?

 arr[l]

or

 arr[1]
 
Nunya business
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Just a note: l (el) is a terrible choice for a variable name.  To illustrate, which of these is correct?

 arr[l]

or

 arr[1]


Didn't even think of that, thanks for the tip!
 
Nunya business
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
first of all: your processSieve method should return a boolean, according to its signature, however you return the input array. That is wrong of course.

Also, when the processSieve is invoked, the parameter 'arr' already references your original 'sieve' array. So, every change you make to that array in 'processSieve' gets reflected in the array 'sieve' as well. So, in fact you do not need to return anything at all.


That makes more sense to me now. Thank you for the comments, I'm starting to get it worked out
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like you're trying to implement the algorithm for the Sieve of Eratosthenes. If so, your implementation is going to fall way short.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!