我是泛型的新手,我正在尝试使用泛型SinglyLinkedNode实现链接列表。但是,当我运行setData()和setNext()方法时,出现以下错误:
.\singlyLinkedList\SinglyLinkedNode.java:13: error: incompatible types: T#1 cannot be
converted to T#2
this.data = d;
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>setData(T#1)
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedNode.java:21: error: incompatible types:
SinglyLinkedNode<T#1> cannot be converted to SinglyLinkedNode<T#2>
this.next = n;
^
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>setNext(SinglyLinkedNode<T#1>)
T#2 extends Object declared in class SinglyLinkedNode
在我看来,引发该错误是因为存在潜在的情况,其中预先存在的this.data或this.next的类型与d或n不匹配。我该如何解决?有什么办法可以用T#2(新数据)覆盖T#1(现有数据)的类型? 这是带有构造函数和设置方法的类:
public class SinglyLinkedNode<T> {
private T data;
private SinglyLinkedNode<T> next;
SinglyLinkedNode(T d) {
this.data = d;
this.next = null;
}
public <T> void setData(T d) {
this.data = d;
}
//...
public <T> void setNext(SinglyLinkedNode<T> n) {
this.next = n;
}
//...
}
感谢您提供的任何帮助。预先感谢!
编辑:谢谢您的帮助!我已经从方法中删除了,但是当我从SinglyLinkedList类中运行setNext()时,仍然出现以下错误:
.\singlyLinkedList\SinglyLinkedList.java:63: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
curr.setNext() = toBeRemoved.getNext();
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedList.java:79: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
prev.setNext() = this.head;
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode
.\singlyLinkedList\SinglyLinkedList.java:82: error: method setNext in class SinglyLinkedNode<T#2> cannot be applied to given types;
prev.setNext() = curr.getNext();
^
required: SinglyLinkedNode<T#1>
found: no arguments
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class SinglyLinkedList
T#2 extends Object declared in class SinglyLinkedNode
因此,我觉得getNext()(来自SinglyLinkedNode)和head(来自SinglyLinkedList)似乎没有返回任何值,即使它们都应该都返回SinglyLinkedNode对象。
这是有问题的代码。我已标记出错误发生的地方。
public class SinglyLinkedList<T> {
private int size;
private SinglyLinkedNode<T> head;
private SinglyLinkedNode<T> tail;
SinglyLinkedList() {
this.size = 0;
this.head = null;
this.tail = null;
}
//...
public SinglyLinkedNode<T> remove(int i) {
SinglyLinkedNode<T> curr = this.head;
for(int counter = 0; counter < i; counter++) {
curr = curr.getNext(); //<---------------
}
SinglyLinkedNode<T> toBeRemoved = curr.getNext();
curr.setNext() = toBeRemoved.getNext();
this.size--;
return toBeRemoved;
}
//...
public SinglyLinkedNode<T> remove(T d) {
if(this.head == null) {
return null;
}
if(this.head.getData() == d) {
SinglyLinkedNode<T> toBeRemoved = this.head;
this.head = this.head.getNext();
return toBeRemoved;
}
SinglyLinkedNode<T> curr = this.head;
SinglyLinkedNode<T> prev = null;
prev.setNext() = this.head; //<---------------
while(curr.getNext() != null) {
if(curr.getData() == d) {
prev.setNext() = curr.getNext(); //<---------------
return curr;
}
}
return null;
}
//...
如果仍然有人看到这篇文章,如果能为我提供帮助,我将不胜感激。再次感谢!
编辑:我也修复了此错误;是
curr.setNext(toBeRemoved.getNext());
不是
curr.setNext() = toBeRemoved.getNext()
答案 0 :(得分:2)
您面临的问题实际上非常简单。基本上,您需要了解泛型方法完全独立于泛型类。
public <T> void setData(T d) {
this.data = d;
}
public <T> void setNext(SinglyLinkedNode<T> n) {
this.next = n;
}
当您将<T>
放在方法的修饰符中时,即表示它是通用方法。泛型方法中的类型T不同,并且覆盖了整个类的类型T,这意味着您可以输入任何类型的对象,并且该方法会将其用作其局部T值,该值可能与全局值完全不同键入T。
从方法修饰符中删除<T>
将使它们成为常规的setter方法,并且默认为类类型T,这保证与所需的类型匹配。
答案 1 :(得分:0)
您不需要在setter上使用泛型,因为它将处理与class相同类型的数据。因此,您的代码将如下所示:
public class SinglyLinkedNode<T> {
private T data;
private SinglyLinkedNode<T> next;
SinglyLinkedNode(T d) {
this.data = d;
this.next = null;
}
public void setData(T d) {
this.data = d;
}
//...
public void setNext(SinglyLinkedNode<T> n) {
this.next = n;
}
//...
}
因此,现在您可以创建SinglyLinkedNode实例,并将值设置为:
public static void main(String[] args) {
SinglyLinkedNode<Integer> integers = new SinglyLinkedNode<>(2);
//
}
此外,由于已经有了用于设置值的构造函数,因此我将摆脱设置方法。