java泛型,未经检查的警告
这里是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
的List对象l被分配给另一个List对象时,就会发生堆污染情况, ls,它有一个不同的静态类型 List
// 这是来自 oracle 教程
我的问题是为什么静态类型是 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
?
多谢 !
here is part of tutorial in oracle page :
Consider the following example:
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
In detail, a heap pollution situation occurs when the List object l, whose static type is List<Number>
, is assigned to another List object, ls, that has a different static type, List<String>
// this is from oracle tutorial
my question would be why is the static type List<Number>
and not just List
??
later another question would be from code of my studies :
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>();
}
Why in this code instead of elArray = new ListaConPI[numVertices+1]
wouldnt we write elArray = new ListaConPI<Adyacente>[numVertices+1]
?
Thanks a lot !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请阅读类型擦除。现在,在删除类型后重新考虑您的代码(我只做第一个示例):
出于同样的原因,您不能拥有这样的类:
Please, read about type erasure. Now, reconsider your code after erasing the types (I'll do only the first example):
For the same reason, you cannot have a class like this:
这样编译器就可以在编译时而不是运行时捕获上述错误。这是泛型的要点。
因为您无法实例化泛型类型的数组(尽管您可以将此类数组声明为变量或方法参数)。请参阅我之前对同一问题的回答。
So that the compiler can catch bugs like the above already at compilation time, instead of runtime. This is the main point of generics.
Because you can't instantiate arrays of generic types (although you can declare such arrays as variables or method parameters). See this earlier answer of mine to the same question.
l 是什么类型?它是一个列表,这是它的静态类型,它可以是任何旧的列表。因此,如果您在编译时分配
编译器无法知道这是否安全。您显示的示例表明它是不安全的,并且会产生 ClassCastException。
What is the type of l? Its a List, that's its static type, it could be any old List. Hence if you assign
The compiler at compile time cannot know whether this is safe. The example you show demonstrates that it is unsafe, and the ClassCastException results.