标签:java泛型
泛型 ``
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("liwen");
list.add("jiayou");
show(list);
}
public static void show(List list)
{
for(Object obj:list)
{
String s = (String) obj;
System.out.println(s + "\t");
}
}
` public static void main(String[] args) {
Boolean numA = false;
Boolean numB = true;
System.out.println("numA" + numA);
System.out.println("numB" + numB);
swap(numA,numB);
}
//泛型方法
public static <T> void swap(T numA,T numB) {
T temp = numA;
numA = numB;
numB = temp;
System.out.println("numA" + numA);
System.out.println("numB" + numB);
}`
//泛型类
public class Apple<T> {
//泛型方式
public void buy(T t){
System.out.println("苹果:" + t + "信息");
}
}
使用:
Apple<Integer> a1 = new Apple<Integer>();
a1.buy(10);
Apple<Double> a2 = new Apple<Double>();
a2.buy(5.5);
通配符
public static void main(String[] args) {
List<Boolean> booleanList = new ArrayList<Boolean>();
booleanList.add(true);
booleanList.add(false);
show(booleanList);
}
public static void show(List<?> list) {
System.out.println("集合长度为:" + list.size());
for(Object o : list){
System.out.println(o);
}
}
排序:
List<String> stringList = new ArrayList<String>();
stringList.add("one");
stringList.add("two");
stringList.add("three");
System.out.println("倒序前:");
for(String s : stringList){
System.out.print(s+"\t");
}
reverse(stringList);
System.out.println("\n倒序后:");
for(String s : stringList){
System.out.print(s+"\t");
}
}
//倒序集合
public static void reverse(List<String> stringList) {
//集合非空且至少有2个值
if(stringList!=null && stringList.size()>=2){
Collections.reverse(stringList);
}
}
标签:java泛型
原文地址:http://blog.51cto.com/357712148/2103800