• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
     
    Matthew Brown
    Bartender
    Posts: 4549
    8
    Java Netbeans IDE Scala
    • 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20369
    44
    Chrome Eclipse IDE Java Windows
    • 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20369
    44
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 692
    Eclipse IDE Java Linux
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic