示例:
public class Apple {
private static long counter;
private final long id = counter++; public long id(){
return id;
}
}
public class Orange { }
public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
//定义一个ArrayList容器, 不指定其类型
ArrayList apples = new ArrayList();
for (int i = 0; i < 3; i++) {
apples.add(new Apple());
} apples.add(new Orange()); //此时, apples容器中存在4个对象, 其中前三个为Apple类型, 最后一个为Orange类型
for (int i = 0; i < apples.size(); i++) {
//get()方法取值时, 得到的只是Object的引用, 必须将其强制转型为Apple, 否则编译错误
//当试图将Orange对象转型为Apple时, 发生类型转换异常
System.out.println(((Apple)apples.get(i)).id());
/*
* output:
* 0
* 1
* 2
* */
}
}
}
在本例中,因为ArrayList保存的是Object,所以可以将Apple对象和Orange对象放进容器中,当在使用ArrayList的get()方法来取出Apple对象时,得到的只是Object的引用,必须将其转型为Apple,因此,需在调用Apple的id()方法之前,强制进行转型,否则,
就会得到编译错误。
刚才声明容器时没有预先定义类型,默认为Object,现在使用预定义泛型来看看:
public class ApplesAndOrangesWithoutGenerics2 {
public static void main(String[] args) {
//定义一个保存Apple对象的ArrayList, 尖括号括起来的是类型参数
//它指定了这个容器示例可以保存的类型, 通过使用泛型, 就可以在编译器放置将错误类型的对象放置到容器中
ArrayList<Apple> apples = new ArrayList<Apple>();
for (int i = 0; i < 3; i++) {
apples.add(new Apple());
} //apples.add(new Orange()); 编译器可以阻止将Orange放置到apples中 for (int i = 0; i < apples.size(); i++) {
System.out.println(apples.get(i).id());
} for (Apple c : apples) {
System.out.println(c.id());
} /*output
* 0
* 1
* 2
* 0
* 1
* 2
* */
}
}
我们注意到,定义了容器类型后,编译器可以阻止将Orange放置到apples中,因为此时Orange对象的类型与容器类型不匹配,发生编译错误;另外,将元素从容器中取出时,类型转换也不再时必须的了,因为容器知道自己保存的是什么类型,因此会在调用
get()时帮忙转型。