File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to find a single false in boolean array full of trues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to find a single false in boolean array full of trues" Watch "how to find a single false in boolean array full of trues" New topic
Author

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

Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
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

Joined: Jun 09, 2003
Posts: 4632
maybe something like this??

Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to find a single false in boolean array full of trues
 
Similar Threads
how to while loop with the condition being multiple elements of an array?
maze solve
Array element
Check an array for equality
Please explain. Boolean[] b1 = new Boolean[10];