1. <? super T> 表示以T类型为下限的某种类
实例化的时候对象的泛型一定要大于等于T类型(也就是T的父类或自己)
使用的时候类型一定要小雨等于T类型(也就是T的子类或自己)
2. <? extends T> 表示以T类型为上线的某种类
实例化的时候对象的泛型一定要小于等于T类型(也就是T的子类或自己)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Searching {
public static void main(String[] args) {
String[] strings = { "0", "1", "2", "3", "4", "5"};
List<Integer> integers = new ArrayList<Integer>();
for(String s : strings){
integers.add(Integer.valueOf(s));
}
if(integers.get(1)>new Integer(1)){System.out.println("<1>");}
if(integers.get(1)<new Integer(1)){System.out.println("<2>");}
if(integers.get(1)==new Integer(1)){System.out.println("<3>");}
System.out.println(Collections.binarySearch(integers, 1,cmp));
}
static Comparator<Integer> cmp = new Comparator<Integer>(){
public int compare(Integer i,Integer j){
return i<j?-1:(i==j?0:1);
}
};
}
此程序输出:-2
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Searching {
public static void main(String[] args) {
String[] strings = { "0", "1", "2", "3", "4", "5"};
List<Integer> integers = new ArrayList<Integer>();
for(String s : strings){
integers.add(Integer.valueOf(s));
}
if(integers.get(1)>new Integer(1)){System.out.println("<1>");}
if(integers.get(1)<new Integer(1)){System.out.println("<2>");}
if(integers.get(1)==new Integer(1)){System.out.println("<3>");}
System.out.println(Collections.binarySearch(integers, 1,cmp));
}
static Comparator<Integer> cmp = new Comparator<Integer>(){
public int compare(Integer i,Integer j){
return i<j?-1:(i>j?1:0);
}
};
}
此程序输出:1
|