我在CircularLinkedList中遇到泛型问题。我有我的CircularLinkedList类,一个Node类和一个Person类。
节点应包含人员,而CircularLinkedList应包含那些节点。问题是,当我尝试在Test类中创建我的CircularLinkedList时,出现错误消息:
绑定不匹配:类型Node<Person>
不能有效替代
类型的有界参数<E extends Comparable<? super E>>
CircularLinkedList<E>
你能看一下我的仿制药吗?
CircularLinkedList.java
package cirkulærliste;
import java.util.Random;
public class CircularLinkedList<E extends Comparable<? super E>> {
private Node<E> next;
private Node<E> start;
private int size = 0;
public CircularLinkedList() {
setNext(null);
}
/**
* tilføjer personer
* @param p
*/
public void addPerson(E e) {
Node<E> newNode = new Node<E>(e, null);
Node<E> tempNext = next;
if(next == null){
next = newNode;
next.setNext(next);
} else if(size > 1){
}
}
/**
* udskriver personerne i den rækkefølge de står i listen
*/
public void print() {
Node<E> tempNode = start;
while (!tempNode.getNext().equals(start)) {
System.out.println(tempNode);
tempNode = tempNode.getNext();
}
System.out.println(tempNode);
}
/**
* en tilfældig person i den cirkulæreliste vælges som start i listen
*/
public void randomStart() {
int size = 1;
Node<E> tempNode = next.getNext();
while (!tempNode.getNext().equals(next.getNext())) {
tempNode = tempNode.getNext();
size++;
}
Random randomizer = new Random();
int chosen = randomizer.nextInt(size);
for (int i = 0; i <= chosen; i++) {
tempNode = tempNode.getNext();
}
start = tempNode;
}
/**
* fjerner den person fra listen der ligger count pladser fra start i listen. Personen der fjernes returneres.
* @param count
*/
public Node<E> remove(int count) {
Node<E> tempNode2;
for (int i = 1; i < 5; i++) {
next = next.getNext();
}
tempNode2 = next.getNext();
next.setNext(next.getNext().getNext());
tempNode2.setNext(null);
return tempNode2;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node<E> getNext() {
return next;
}
public void setStart(Node<E> start) {
this.start = start;
}
public Node<E> getStart() {
return start;
}
}
节点java
package cirkulærliste;
public class Node<E> {
private E element;
private Node<E> next;
public Node(){
element = null;
next = null;
}
public Node(E element, Node<E> next){
this.setElement(element);
this.next = next;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> newNode) {
this.next = newNode;
}
public String toString(){
return "" + element;
}
}
人.java
package cirkulærliste;
public class Person implements Comparable<Person> {
private String name;
private int number;
public Person(String name, int number){
this.setName(name);
this.setNumber(number);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "Name: " + name + " - " + "Number: " + number;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public int compareTo(Person o) {
// TODO Auto-generated method stub
return 0;
}
}
Test.java
public class Test {
public static void main(String[] args) {
CircularLinkedList<Person> list = new CircularLinkedList<Person>();
Node<Person> n1 = new Node<Person>();
n1.setElement(new Person("Thomas", 1));
list.addPerson(n1); // Compile error occurs here
}
}
最佳答案
听起来你在写
CircularLinkedList<Node<Person>>
在您的测试课中;但是你实际上需要写
CircularLinkedList<Person>
更新更新的问题:您还需要更改此内容:
Node<Person> n1 = new Node<Person>();
n1.setElement(new Person("Thomas", 1));
list.addPerson(n1); // Compile error occurs here
对此:
list.addPerson(new Person("Thomas", 1));
通常,使用
CircularLinkedList
的代码不应引用Node
。 CircularLinkedList
在内部使用Node
,但是该类的用户无需担心。同样,您的通用
CircularLinkedList
类具有一个名为addPerson
的方法,这有点奇怪。当然应该将其称为addElement
或仅仅是add
吗?