讨论试题代码求助

头像
逍遥
52阅读0评论

请分析下面的这段代码(f1方法)完成的功能是什么?

private  static <T>  int f1(List<? extends Comparable<? super T>> list, T key)  
{  
    int low = 0;  
 int high = list.size()-1;  
 ListIterator<? extends Comparable<? super T>> i = list.listIterator();  
  
 while (low <= high) {  
        int mid = (low + high) >>> 1;  
 Comparable<? super T> midVal = get(i, mid);  
 int cmp = midVal.compareTo(key);  
  
 if (cmp < 0)  
            low = mid + 1;  
 else if (cmp > 0)  
            high = mid - 1;  
 else return mid;  
 }  
    return -(low + 1);  
}  
private static <T> T get(ListIterator<? extends T> i, int index) {  
    T obj = null;  
 int pos = i.nextIndex();  
 if (pos <= index) {  
        do {  
            obj = i.next();  
 } while (pos++ < index);  
 } else {  
        do {  
            obj = i.previous();  
 } while (--pos > index);  
 }  
    return obj;  
}
讨论话题:
工作&职场
收藏
举报
加载中…
精选评论
暂无数据
暂无数据