Java泛型,未经检查的警告 [英] java generics, unchecked warnings

查看:295
本文介绍了Java泛型,未经检查的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是oracle页面中教程的一部分:

考虑下面的例子:

  List l = new ArrayList< Number>(); 
列表< String> ls = 1; // unchecked warning
l.add(0,new Integer(42)); //另一个未经检查的警告
String s = ls.get(0); //抛出ClassCastException

详细地说,当List对象l静态类型是 List< Number> ,被分配给另一个具有不同静态类型的List对象ls, List< String>

我的问题是为什么静态类型 List< Number> 而不仅仅是 List ??
以后的另一个问题将来自我的研究代码:

  public class GrafoD extends Grafo {

保护int numV,numA;
protected ListaConPI< Adyacente> elArray [];

* / **构造符合格拉诺数值顶点数目*
* @参数numVertices:数字顶点格拉布
* /
@SuppressWarnings(未经检查)
public GrafoD(int numVertices){
numV = numVertices; NUMA = 0;
elArray = new ListaConPI [numVertices + 1]; (int i = 1; i <= numV; i ++)时的
elArray = new LEGListaConPI< Adyacente>();
}

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



非常感谢!

解决方案


我的问题是为什么静态键入列表<数字> 而不仅仅是列表


以便编译器可以在编译时捕获类似上面的错误,而不是运行时。这是泛型的主要观点。


为什么在此代码中而不是 elArray = new ListaConPI [numVertices + 1 ] wouldnt we write elArray = new ListaConPI< Adyacente> [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 !

解决方案

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.

这篇关于Java泛型,未经检查的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆