public static void printIterator0(ArrayList<Integer> al) {
Iterator<Integer> it = al.iterator();
while(it.hasNext()) {
Integer next = it.next();
String string = next.toString();
System.out.println(string);
}
}
/**
* 通配符方式。
* 缺点:不能使用对象中元素的特有方法了。
* */
public static void printIterator(ArrayList<?> al) {
Iterator<?> it = al.iterator();
while(it.hasNext()) {
Object next = it.next();
System.out.println(next);
}
}
/**
* 泛型方式
* 缺点:不能使用对象中元素的特有方法了。
* @param <T>
* */
public static <T> void printIterator2(ArrayList<T> al) {
Iterator<T> it = al.iterator();
while(it.hasNext()) {
Object next = it.next();
System.out.println(next);
}
}
7.向上限定ArrayList<? super E>集合中只能存放E或者E的父类
8.向下限定ArrayList<? extends E>集合中只能存放E或者E的子类
原文:http://blog.51cto.com/13579086/2070570