我正在学习Java泛型。为此,我尝试了一个简单的LinkedList。
pip install theia
但是当所有方法确实存在于Node类中时,我都会收到此错误。我在做什么错?
class Node {
private int age;
private String name;
private Node next;
public Node(int age,String name) {
this.age = age;
this.name = name;
this.next = null;
}
public int getage() {
return this.age;
}
public String getName() {
return this.name;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkedList<T> {
private T head;
private T current;
public LinkedList() {
head = null;
current = null;
}
public void append(T x) {
if (head == null) {
head = x;
current = x;
}
else {
current.setNext(x);
current = x;
}
}
public T getat(int index) {
T ptr = head;
for(int i = 0; i < index; i++) {
ptr = ptr.getNext();
}
return ptr;
}
}
class Main {
public static void main(String[] args) {
LinkedList<Node> list = new LinkedList<Node>();
list.append(new Node(39,"John"));
list.append(new Node(43,"Josh"));
Node x = list.getat(1);
System.out.println(String.format("%d,%s",x.getage(),x.getName()));
}
}