Topic: 关于binarySearch()的返回值

  Print this page

1.关于binarySearch()的返回值 Copy to clipboard
Posted by: AchillesA
Posted on: 2004-09-07 16:32


import java.util.*;
import com.bruceeckel.util.*;
class E_13_2 implements Comparable
{
private String s1;
private String s2;
E_13_2(){
}
E_13_2(String s1,String s2){
this.s1=s1;
this.s2=s2;
}
public boolean equals(Object o){
return (o instanceof E_13_2)&&(s2==((E_13_2)o).s2);
}
public int compareTo(Object obj)
{
String s = ((E_13_2)obj).s1;
return s1.compareTo(s);
}
public String toString(){
return "[s1:"+s1+"<>s2:"+s2+"]";
}
public static Arrays2.RandStringGenerator gen=
new Arrays2.RandStringGenerator(8);
public static Generator generator(){
return new Generator(){
public Object next(){
return new E_13_2((String)gen.next(),(String)gen.next());
}
};
}
}
public class E13Test
{
public static void main(String args[]){
E_13_2 e = new E_13_2();
E_13_2[] e13 = new E_13_2[10];
Arrays2.fill(e13,E_13_2.generator());
Arrays2.print("before sort() =>",e13);
int i=Arrays.binarySearch(e13,e13[4]);
System.out.println("binarySearch=>"+i);
Arrays.sort(e13);
Arrays2.print("after sort() =>",e13);
}
}

这里的binarySearach()的返回值为什么一会儿是正数,一会儿又是负数?而且负数的值也不同。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923