import java.util.*;
class CollectionsDemo
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
List<String> list=new ArrayList<String>();
list.add("abcd");
list.add("aaa");
list.add("zz");
list.add("kkkkk");
list.add("qq");
list.add("z");
sop(list);
//sop(Collections.sort(list));
//sop(list);
sop(halfSearch1(list,"bb"));
}
public static <Y> int halfSearch1(List<Y> List,Y key)//此处的变量类型该怎么加,我加了与集合一样的类型一直报错。
{
int max,mid,min;
max=List.size()-1;
min=0;
while(min<max)
{
mid=(min+max)>>1;
Y t=List.get(mid);
int num=t.compareTo(key);
if(num>max)
max=mid-1;
else if(mid<0)
min=mid+1;
else
return mid;
}
return -min-1;
} |
|