简体   繁体   English

为什么我收到警告 [unchecked] unchecked 转换?

[英]why am i getting the warning [unchecked] unchecked conversion?

Can anyone please point me what I am missing over in the below statements?任何人都可以指出我在下面的陈述中遗漏了什么吗?
Warning 1警告 1

 prog.java:16: warning: [unchecked] unchecked conversion
        adj = new LinkedList[v];
              ^
  required: LinkedList<Integer>[]
  found:    LinkedList[]

Warning 2警告 2

prog.java:18: warning: [unchecked] unchecked conversion
            adj[i] = new LinkedList();
                     ^
  required: LinkedList<Integer>
  found:    LinkedList

Warning 3警告 3

prog.java:43: warning: [unchecked] unchecked call to push(E) as a member of the raw type Stack
        stack.push(new Integer(v));
                  ^
  where E is a type-variable:
    E extends Object declared in class Stack

help me to recover this warning.Thanks in advance帮我恢复这个警告。提前致谢
PC :- keep_smiling PC :- 保持微笑

warning 1 and 2 arises because you are not using generics properly.Cast your LinkedList to a generic type rather than raw type eg LinkedList <Integer>.出现警告 1 和 2 是因为您没有正确使用泛型。将您的 LinkedList 转换为泛型类型而不是原始类型,例如LinkedList <Integer>.

I cant tell about the third as you should provide the code tempelate.我不能说第三个,因为你应该提供代码模板。

Because you are using generic data-structures and your variables do not match the exact generic type where you are receiving the warnings.因为您使用的是通用数据结构,并且您的变量与您收到警告的确切通用类型不匹配。 To be more accurate, it happens when the right-hand-side generic type is not the same or a sub-type of the left-hand-side generic type.更准确地说,它发生在右侧泛型类型与左侧泛型类型不同或子类型不同时。

For example if you have:例如,如果您有:

List<String> x;

If you do something like:如果您执行以下操作:

x = new ArrayList();

You will get an unchecked warning, because x 's generic type, ie String , is not the same or a super-type of the right-hand-side generic type Object which is the default for List.您将收到未经检查的警告,因为x的泛型类型,即String ,与右侧泛型类型Object ,或不是 List 的默认类型的超类型。

just put就放

@SuppressWarnings("unchecked") 

above your method;高于你的方法;

For example:例如:

class Graph { 
    private int V;
    private LinkedList <Integer> adj[]; 

    @SuppressWarnings("unchecked")
    Graph(int v) { 
       V = v; 
       adj = new LinkedList[v];
       for (int i=1; i<=v; i++) 
          adj[i] = new LinkedList<Integer>(); 
    } 
}

There is one problem here , when we use这里有一个问题,当我们使用

adj = new LinkedList<Integer>[v]; rather than adj = new LinkedList[v]

the compiler throws error cannot use generic type, so by adding above condition @SuppressWarnings("unchecked") solves this problem.编译器抛出错误不能使用泛型类型,因此通过添加上述条件 @SuppressWarnings("unchecked") 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM