Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
     
    Bartender
    Posts: 4568
    9
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 22645
    124
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 22645
    124
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Yup, yup, yup. Tiny ad:
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic