【发布时间】:2015-07-21 18:15:03
【问题描述】:
我知道这个问题之前已经回答过了,我已经阅读了 How to properly define an array of linked list in Java ? 和 How do I fix "The expression of type List needs unchecked conversion...'? 中的答案
我使用这个,正如第二个链接的第二个答案中所建议的那样
@SuppressWarnings("unchecked")
public void myMethod()
{
//...
}
因为在我的代码(下面的示例)中,我需要使用静态 LinkedList[],因此在读取数据后,我将获得我将在“Q = new LinkedList[size of data];”中使用的数字大小.我需要静态 LinkedList[] 因为我在许多功能中使用它
static LinkedList<Integer>[] Q;
@SuppressWarnings("unchecked")
static public void function(){
int r=3;
Q = new LinkedList[10];//this gives the warnning without "the unchecked"
Q[r]=new LinkedList<Integer>();
}
static public void function2(){
//using Q[r] for different r
}
当我编译时它仍然给我警告
memoria/bosques.java:3150: warning: [rawtypes] found raw type: LinkedList
Q = new LinkedList[10];
^
missing type arguments for generic class LinkedList<E>
where E is a type-variable:
E extends Object declared in class LinkedList
在windows的eclipse中编译这个效果很好,但是在linux中不行
有什么我可以做的吗?
【问题讨论】:
-
为什么不切换到
List<List<E>>之类的东西? -
仅仅因为您关闭了未经检查的转换的警告并不意味着您应该使用原始类型。使用
Q = new LinkedList<Integer>[10]。 -
@Jishnu Prathap 我用静态 LinkedList
[] Q; 声明它 -
@John Bollinger Q = new LinkedList
[10] 这是不允许的(因此是一个错误)
标签: java arrays linked-list