这是 oracle 页面中教程的一部分:
考虑以下示例:
List l = new ArrayList<Number>();
List<String> ls = l; // unchecked warning
l.add(0, new Integer(42)); // another unchecked warning
String s = ls.get(0); // ClassCastException is thrown
List<Number>
详细来说,当静态类型为 的 List 对象 l被分配给另一个具有不同静态类型的 List 对象 ls时,就会发生堆污染情况, List<String>
// 这是来自 oracle 教程
我的问题是为什么是静态类型List<Number>
而不仅仅是List
?后来另一个问题来自我的研究代码:
public class GrafoD extends Grafo {
protected int numV, numA;
protected ListaConPI<Adyacente> elArray[];
*/** Construye un Grafo con un numero de vertices dado*
* @param numVertices: numero de Vertices del Grafo
*/
@SuppressWarnings("unchecked")
public GrafoD(int numVertices){
numV = numVertices; numA=0;
elArray = new ListaConPI[numVertices+1];
for (int i=1; i<=numV; i++) elArray= new LEGListaConPI<Adyacente>();
}
为什么在这段代码中而不是elArray = new ListaConPI[numVertices+1]
我们不会写elArray = new ListaConPI<Adyacente>[numVertices+1]
?
非常感谢 !