java泛型,未经检查的警告

发布于 2024-11-28 23:46:30 字数 1150 浏览 1 评论 0原文

这里是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[numVertices+1] ]

多谢 !

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮酒 2024-12-05 23:46:31

请阅读类型擦除。现在,在删除类型后重新考虑您的代码(我只做第一个示例):

List l = new ArrayList(); // Compiler remembers that it should check that only numbers can be added
List ls = l; // Compiler remembers that it should cast everything back to a String
l.add(0, new Integer(42)); // the compiler checks that the second parameter is a Number.
String s = ls.get(0); // The compiler casts the result back to a String, so you get an exception

出于同样的原因,您不能拥有这样的类:

class A<T> {
    public void method(T obj) { }
    public void method(Object obj) { }
}

Please, read about type erasure. Now, reconsider your code after erasing the types (I'll do only the first example):

List l = new ArrayList(); // Compiler remembers that it should check that only numbers can be added
List ls = l; // Compiler remembers that it should cast everything back to a String
l.add(0, new Integer(42)); // the compiler checks that the second parameter is a Number.
String s = ls.get(0); // The compiler casts the result back to a String, so you get an exception

For the same reason, you cannot have a class like this:

class A<T> {
    public void method(T obj) { }
    public void method(Object obj) { }
}
沉鱼一梦 2024-12-05 23:46:30

我的问题是为什么静态类型是 List 而不仅仅是 List

这样编译器就可以在编译时而不是运行时捕获上述错误。这是泛型的要点。

为什么在这段代码中我们不写 elArray = new ListaConPI[numVertices+1] 而不是 elArray = new ListaConPI[numVertices+1]? p>

因为您无法实例化泛型类型的数组(尽管您可以将此类数组声明为变量或方法参数)。请参阅我之前对同一问题的回答

my question would be why is the static type List<Number> and not just List?

So that the compiler can catch bugs like the above already at compilation time, instead of runtime. This is the main point of generics.

Why in this code instead of elArray = new ListaConPI[numVertices+1] wouldnt we write elArray = new ListaConPI<Adyacente>[numVertices+1]?

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.

初懵 2024-12-05 23:46:30
List l = // something;

l 是什么类型?它是一个列表,这是它的静态类型,它可以是任何旧的列表。因此,如果您在编译时分配

List<String> listOfString = l;

编译器无法知道这是否安全。您显示的示例表明它是不安全的,并且会产生 ClassCastException。

List l = // something;

What is the type of l? Its a List, that's its static type, it could be any old List. Hence if you assign

List<String> listOfString = l;

The compiler at compile time cannot know whether this is safe. The example you show demonstrates that it is unsafe, and the ClassCastException results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文