我對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
是解釋你期望什麼,你得到什麼,而不是你的期望 –