Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to find a single false in boolean array full of trues

 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to solve a problem and I had this as a partial step to my solution. So if you were given a large boolean array fill with all true value except one false hiding within somewhere. Is there any efficient way to search for the index of that false rather than going through the whole array?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe something like this??

 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thank you so much for taking your time to reply. I was aware of the usage of Arrays.asList but was reluctant because I thought it might allocate a brand new array or arraylist and needed to copy everything over again. Upon further reading, on more than one occasions, it appeared people are suggesting no new allocation were taking place other than just creating a specialized view or wrapper to the original array. Therefore minimum hit and memory usage for using Arrays.asList. If that is true then I think your solution is perfect. Thanks again!
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However... I think it's very likely that the "indexOf" method works by "going through" the list, which is something you didn't seem to want in your original post. Or were you just looking for a solution which didn't require you yourself to write the code? If so, then Michael Dunn's solution is just fine.

(And yes, I do believe it's better to look for built-in solutions to ordinary problems like this one, rather than writing your own code. For one thing the built-in solutions already work, whereas code I write doesn't necessarily work the first time and needs to be tested and debugged. For another thing, the built-in solution has a chance of working better than the code I might write.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic