Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe something like this??

 
Tony Smith
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
reply
    Bookmark Topic Watch Topic
  • New Topic