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 Search Element in array Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Search Element in array Problem" Watch "Search Element in array Problem" New topic
Author

Search Element in array Problem

Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44

i am getting Couldn't Find even if i found the element. please help me. How to omit that message for found elements.
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
You can keep a flag whose value will change to say "true" , just before break. If value of this flag is not "true" then print "element not found"


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

And you see- the print- "Couldn't find" executes everytime the code comes out of the loop. So as said by Siddhesh, you would have to use a flag (or even the same index- checking if its reached the end of the array) to print the message- "Couldn't Find"


Mohamed Sanaulla | My Blog
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Also for searching you can use the method Arrays.binarySearch() which uses a fast searching algorithm.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14143
    
  18

Also, if this is real production code (and not just an exercise) - there are more efficient ways of sorting an array than the bubble sort algorithm that you're using. There are methods for sorting arrays in class java.util.Arrays, which implement an optimized quicksort algorithm.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Jesper de Jong wrote:. . . There are methods for sorting arrays in class java.util.Arrays, which implement an optimized quicksort algorithm.
Only for primitives, I see from the API. For Object[] sorting, it uses a merge sort, because merge sort is "stable".
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14143
    
  18

Campbell Ritchie wrote:Only for primitives, I see from the API. For Object[] sorting, it uses a merge sort, because merge sort is "stable".

Aha, ok. "Stable" means that two elements that are equal will not switch order (they will be left in the original order that they were in in the array). Note that merge sort is still more efficient than bubble sort: the API docs mention that it's guaranteed O(N * log(N)) performance, whereas bubble sort has average and worst case performance of O(N^2).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
I had always thought that sort() used merge sort throughout, but that is because I didn't read the whole of the API. That is the correct interpretation of "stable"; since primitives only have one value, there is no need for a stable sort. And bubble sort is only quicker for the very smallest arrays, otherwise it is notorious for its inefficiency.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Search Element in array Problem