Collection Sort, BinarySearch Warning:
ImportJava.util.*; Public classListInterface2 { Public Static voidMain (String []args) {List L1=NewLinkedList (); List L2=NewLinkedList (); for(inti=0; i<=10; i++) {L2.add ("B" +i); } for(inti=0; i<=9; i++) {L1.add ("A" +i); } System.out.println (L2); System.out.println (L1); Collections.shuffle (L1); System.out.println (L1); Collections.reverse (L1); System.out.println (L1); Collections.sort (L1); System.out.println (L1); Collections.reverse (L1);collections.copy (L2,L1); System.out.println (L2); System.out.println (L1); System.out.println (Collections.binarysearch (L1,"A3")); }}
The running results show:
[B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10][A0, A1, A2, A3, A4, A5, A6, A7, A8, A9][a5, A1, A2, A9, A7, A8, A4, A6, A0, A3][A3, A0, A6, A4, A8, A7, A9, A2, A1, A5][a0, A1, A2, A3, A4, A5, A6, A7, A8, A9][a9, A8, A7, A6, A5, A4, A3, A2, A1 , A0, B10][A9, A8, A7, A6, A5, A4, A3, A2, A1, A0]-1
View Code
Except for the last item, the result is different from the expectation. Other consistent. To correctly derive the index value of the Elem, make the following modifications to the code.
ImportJava.util.*; Public classListInterface2 { Public Static voidMain (String []args) {List L1=NewLinkedList (); List L2=NewLinkedList (); for(inti=0; i<=10; i++) {L2.add ("B" +i); } for(inti=0; i<=9; i++) {L1.add ("A" +i); } System.out.println (L2); System.out.println (L1); Collections.shuffle (L1); System.out.println (L1); Collections.reverse (L1); System.out.println (L1); Collections.sort (L1); System.out.println (L1); //Collections.reverse (L1);collections.copy (L2,L1); System.out.println (L2); System.out.println (L1); System.out.println (Collections.binarysearch (L1,"A3")); }}
[B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10][A0, A1, A2, A3, A4, A5, A6, A7, A8, A9][a5, A6, A1, A0, A9, A7, A8, A2, A4, A3][a3, A4, A2, A8, A7, A9, A0, A1, A6, A5][a0, A1, A2, A3, A4, A5, A6, A7, A8, A9][A0, A1, A2, A3, A4, A5, A6, A7, A8 , A9, B10][A0, A1, A2, A3, A4, A5, A6, A7, A8, A9] 3
View Code
The reason for the error, the list Interface in the object, to find, you need to sort first. find based on sorting!
and can only use Collectin.sort (); With collection. Reverse (); There will be a negative index for the case. (For specific reasons, still unknown, please advise.) :) )
The reference section answers:
Returns the index of the search key if the search key is contained in the list, otherwise (-(insertion point)-1). The insertion point is defined as the point at which the key is inserted into the list: the first element index greater than this key , or list.size () if all the elements in the list are smaller than the specified key. Note that this guarantees that if and only if this key is found, the returned value will be >= 0.
Reference: https://zhidao.baidu.com/question/13681318.html
Personal understanding, the sort () method is arranged in a positive order, BinarySearch can find the index value of Elem normally, but using reverse (), BinarySearch () can not find the corresponding elem, so the index value error. But the specific memory distribution and the reason for appearing, also please know the person to pointing. Thank you:)
Java Error (vi)