泛型
public class Demo04 {
public static void main(String[] args) {
ArrayList<Integer> arrayList01 = new ArrayList<>();//泛型为Integer类
arrayList01.add(1);
arrayList01.add(2);
ArrayList<String> arrayList02 = new ArrayList<>();//泛型为String类
arrayList02.add("wh");
arrayList02.add("jy");
showElement(arrayList01);
showElement(arrayList02);
}
/*
public static <E> void showElement(ArrayList<E> arrayList) {//此为含有泛型的方法, 也可解决此问题
for (E e : arrayList) {
System.out.println(e);
}
}
*/
public static void showElement(ArrayList<?> arrayList) { //方法必须为static修饰,否则 需要通过创建该方法的对象才能进行调用
for (Object o : arrayList) {
System.out.println(o); //多态特性,调用的是?所代表的重写过的toString()方法
}
}
}