本帖最后由 刘圣伟 于 2012-8-17 11:42 编辑
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("afdf");
list.add("dfffdf");
list.add("badffafdf");
list.add("cadaffdf");
sop(list);
//sop(Collections.max(list,new StrlenComp()));
Collections.sort(list);
int index = halfSearch(list,"af");
System.out.println(index);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static int halfSearch(List<String> list,String key){
int max ,min,mid;
max = list.size()-1;
min = 0;
while(min<=max);
{
mid = (max+min)>>1;// /2
String str = list.get(mid);
System.out.println(str);
int num = str.compareTo(key);
if(num>0)
max = mid -1;
else if(num<0)
min = mid +1;
else
return mid;
}
return -min-1;
}
}
折半查找,打不出返回值,哪里错了呢?
|
|