| Author |
why collections.binarysearch() returns wrong output for 1st 2 index position ?
|
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
i m performing on one of my arraylist , the array list has 5 word in it say
like
0 cs1 HeadFirstJava-2ndEdition2 Herbert_Schildt_-_Java_2_The_Complete_Reference3 New folder4 os by galvin5 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: 3793
|
|
|
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: 19216
|
|
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
How To Ask Questions How To Answer Questions
|
 |
naved momin
Ranch Hand
Joined: Jul 03, 2011
Posts: 675
|
|
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: 19216
|
|
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: 675
|
|
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
|
 |
 |
|
|
subject: why collections.binarysearch() returns wrong output for 1st 2 index position ?
|
|
|