我对Java很新,我试图在java中实现一个通用LinkedList类。下面是代码,但它并不正确。本学期我有一些额外的空闲时间,并希望使用这个通用链表来解决我的面试考试准备书中的链表编程挑战。我在这里做错了什么?为什么不按照我想要的方式工作?实现一个通用类型Java LinkedList
感谢您的帮助提前。
public class LinkedList {
public static linkedlist ll;
public static void main(String[] args) {
// TODO Auto-generated method stub
ll = new linkedlist();
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
System.out.println("s");
}
public static class linkedlist<T>{
public Node head;
public Node tail;
int size;
@SuppressWarnings("unchecked")
public linkedlist(){
size = 0;
}
void add(Class<T> typeParameterClass){
if(head == null){
head = new Node(typeParameterClass);
}
Node temp = new Node(typeParameterClass);
Node headCopy = head;
if(headCopy != null){
while(headCopy.getNext()!= null){
headCopy = headCopy.getNext();
}
headCopy.setNext(temp);
}
size++;
}
}
public static class Node<T>{
//final Class<T> typeParameterClass;
Class<T> value;
int intValue;
Node next = null ;
Node prev = null;
public Node(Class<T> typeParameterClass){
value = typeParameterClass;
}
public Node(int i) {
intValue = i;
// TODO Auto-generated constructor stub
}
public Node getNext() {
// TODO Auto-generated method stub
return next;
}
public Node getPrev() {
return prev;
}
public void setNext(Node temp){
next = temp;
}
}
}
请准确描述哪种方式不符合您的要求。 – Eran
是解释你期望什么,你得到什么,而不是你的期望 –