[英]Generic type-Object type mismatch in java
I tried to create a LinkedList class. 我试图创建一个LinkedList类。 But I had 2 problem:
但是我有两个问题:
1)The Node last=null
declaration is giving me a raw-type error but in above of that declaration there is no error like it. 1)
Node last=null
声明给了我一个原始类型的错误,但是在该声明的上方没有类似的错误。 4 same declarations but only the last one gives an error. 4个相同的声明,但只有最后一个给出错误。
2) In the get()
method I want to return V type and as you can see the value variable is already in V type. 2)在
get()
方法中,我想返回V型,并且您可以看到value变量已经在V型中。 But it gives me "cannot convert Object to V" error. 但这给了我“无法将对象转换为V”的错误。 But temp.value is already V.
但是temp.value已经是V。
public class Linkedlist<V> {
public class Node <V> {
private Node next=null;
private String key;
private int size;
private V value=null;
public Node(V value, String key){
this.key=key;
this.value=value;
}
}
Node root=null;
Node temp=null;
Node temp1=null;
Node last=null;
last=root;
public void add(V value, String key){
last.next = new Node(value,key);
last=last.next;
}
public void remove(String key){
temp=root;
if(isEmpty())
System.out.println("list is empty!");
else{
if(temp.next!=null){
if(!temp.next.key.equals(key)){
remove(temp.next.key);
}
else if(temp.next.key.equals(key)){
if(temp.next==last)
last=temp;
temp.next=temp.next.next;
}
}
else
System.out.println("there is no such element");
}
}
public V get(String key){
temp=root;
if(temp.key.equals(key)){
if(temp.next!=null)
get(temp.next.key);
else
return null;
}
else if(temp.key.equals(key))
return temp.value;
}
The two problems you cite are really one and the same. 您引用的两个问题实际上是相同的。 Given a parameterized class
Node<V>
such as you declared, these ... 给定您声明的参数化类
Node<V>
,这些...
Node root=null;
Node temp=null;
Node temp1=null;
Node last=null;
... all declare objects of the raw type Node
. ...全部声明原始类型为
Node
对象。 Other code will interpret treat them as if their type parameters had been specified as Object
. 其他代码将把它们视为已将其类型参数指定为
Object
。 You should instead declare them like so: 您应该这样声明它们:
Node<V> root=null;
Node<V> temp=null;
Node<V> temp1=null;
Node<V> last=null;
where the <V>
is literal -- a reference to the class's type parameter, not any concrete type. 其中
<V>
是文字的-引用类的type参数,而不是任何具体类型。 If you do that then both your errors will go away. 如果这样做,那么两个错误都会消失。
Your inner class Node
is generic, but you're using the raw form of the class. 您的内部类
Node
是通用的,但是您使用的是该类的原始形式。 That means that a method that returns V
gets type-erased, and it is now returning Object
. 这意味着返回
V
的方法被类型擦除,并且现在返回Object
。 But the Node
class doesn't need to be generic. 但是
Node
类不需要是通用的。 A non- static
nested class (ie a nested class) can use its enclosing class's generic type parameter. 非
static
嵌套类(即嵌套类)可以使用其封闭类的泛型类型参数。 So, remove the <V>
on the Node
class definition. 因此,删除
Node
类定义上的<V>
。
public class Node {
Other problems I see: 我看到的其他问题:
last=root;
appears to be outside any constructor, method, or initialization block. isEmpty()
method, but you may have not posted it for brevity. isEmpty()
方法,但是为了简洁起见,您可能还没有发布它。 get()
method needs a return
statement in the case that neither the if
nor the else
conditions are met. if
和else
条件都不满足, if
get()
方法需要return
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.