*
The moose likes Java in General and the fly likes why collections.binarysearch() returns wrong output for 1st 2 index position ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why collections.binarysearch() returns wrong output for 1st 2 index position ?" Watch "why collections.binarysearch() returns wrong output for 1st 2 index position ?" New topic
Author

why collections.binarysearch() returns wrong output for 1st 2 index position ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

i m performing on one of my arraylist , the array list has 5 word in it say
like
  • 0 cs
  • 1 HeadFirstJava-2ndEdition
  • 2 Herbert_Schildt_-_Java_2_The_Complete_Reference
  • 3 New folder
  • 4 os by galvin
  • 5 sample q's


  • when i m entering input say the words which are at index position 2 , 3 , 4 , 5 it gives me the right answer but when i enter word which at index 0 , 1 for 0 it gives -5 and for index 1 it gives -1
    but i know why it is giving me this because he is not able to find the word in a list but i know this word are there in the list
    what can i do to correct this ?

    i know i can perform some math thing to get the right answer on this but without math can i get the right answer ?


    The Only way to learn is ...........do!
    Visit my blog http://inaved-momin.blogspot.com/
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4343
        
        8

    A binary search assumes that your list is already sorted, otherwise the results are unpredicatable. If those are the actual contents of the list - remember that upper- and lower-case characters are sorted separately.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    Try the other binarySearch method, with a comparator:
    However, keep in mind that binarySearch must use the same ordering as the array you're searching.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 688

    Matthew Brown wrote:A binary search assumes that your list is already sorted, otherwise the results are unpredicatable. If those are the actual contents of the list - remember that upper- and lower-case characters are sorted separately.

    thanks i got that
    one more thing i want to download directory from ftp server
    which means directory along with all files in that directory , i m using file tranfer mode ascii do i need to change it to some other thing to get this thing done ?

    i m using libraries from apache common net
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    Naved, please start creating new threads for new problems. The latest problem has nothing to do with the binary search problem.

    But to answer your questions: ASCII transfer mode should never be used for non-text files. If you have binary files you should use binary transfer mode for those. Fortunately, you can also use binary mode for text files.
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 688

    Rob Spoor wrote:Naved, please start creating new threads for new problems. The latest problem has nothing to do with the binary search problem.

    But to answer your questions: ASCII transfer mode should never be used for non-text files. If you have binary files you should use binary transfer mode for those. Fortunately, you can also use binary mode for text files.

    thanks and next time i will keep in mind
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: why collections.binarysearch() returns wrong output for 1st 2 index position ?
     
    Similar Threads
    (anti)clockwise rotation of array elements...HELP!
    Collections.sort() and reverse()
    Hibernate returning List with NULL values
    BinarySearch() Question
    Deranged permutations