【问题标题】:Define an array of LinkedList in java used of @SuppressWarnings("unchecked")在使用@SuppressWarnings("unchecked")的java中定义一个LinkedList数组
【发布时间】: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&lt;List&lt;E&gt;&gt;之类的东西?
  • 仅仅因为您关闭了未经检查的转换的警告并不意味着您应该使用原始类型。使用Q = new LinkedList&lt;Integer&gt;[10]
  • @Jishnu Prathap 我用静态 LinkedList[] Q; 声明它
  • @John Bollinger Q = new LinkedList[10] 这是不允许的(因此是一个错误)

标签: java arrays linked-list


【解决方案1】:

最后,我没有使用 @SuppressWarnings("unchecked"),而是使用了 ArrayList >,所以我没有定义 Q = new LinkedList[4],而是添加了新的 LinkedList:

        static ArrayList<LinkedList<Integer>> Q2 = new ArrayList<LinkedList<Integer>>();

        public static void function(){
        for(int i=0;i<4; i++){
                            LinkedList<Integer> fila=new LinkedList<Integer>();
                            Q2.add(fila);
        }
                        Q2.get(0).add(3);
                        Q2.get(0).add(4);
                        Q2.get(0).add(5);
                        Q2.get(2).add(13);
                        Q2.get(2).add(14);
                        Q2.get(2).add(15);

                        for(LinkedList<Integer> fila:Q2){
                            System.out.print("Miembros");

                            for(Integer it:fila){
                                System.out.print("->{");
                                System.out.print(it);
                                System.out.print("}");
                            }
                            System.out.println("");
                        }

        }

【讨论】:

  • @Jishnu Prathap,你是什么意思?我用 Q.get(r) 改变了我所有的 Q[r] 结构并且工作方式相同,它删除了未经检查的警告......ArrayList 是解决我的问题的一个相关因素,我举了一个关于它如何工作的小例子或者你这么说是因为我问过@SuppressWarnings("unchecked")?
  • 您在创建linkedlist 数组时遇到编译错误,您通过创建linkedlist 的arraylist 解决了这个问题,对吗?实际上,我认为这是您的程序的不同实现,没有编译警告但没有回答您的问题。
猜你喜欢
  • 2012-10-11
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多