Scanner sc2 = new Scanner(System.in);
Ticket tk2 = new Ticket();
Flight ff2 = new Flight();
Flight tmp = new Flight();
System.out.print("Enter ticket ID to cancel: ");
tk2.id = Integer.parseInt(sc2.nextLine());
System.out.print("Enter Flight ID of this ticket: ");
ff2.id = Integer.parseInt(sc2.nextLine());
if (listFlight.check(ff2)) {
tmp = listFlight.get(ff2);
//tmp.tickets.delete(tk2);
//System.out.println("Deleted");
這裏listFlight類和獲取函數:鏈接列表的返回值?
public T get(T el){
SLLNode<T> tmp;
for (tmp = head; tmp != null && tmp.info.equals(el); tmp = tmp.next);
return (T)tmp;
}
它顯示錯誤:
Exception in thread "main" java.lang.ClassCastException: fullversion.SLLNode cannot be cast to fullversion.Flight at fullversion.FullVersion.main(FullVersion.java:90)
@Override
public boolean equals(Object obj){
Flight s = (Flight)obj;
return this.id == s.id;
}
我不知道爲什麼我不能用tmp = listFlight.get(ff2)
。有人可以解釋嗎?
SLLNode
:
public class SLLNode<T> {
public T info;
public SLLNode<T> next;
public SLLNode(){
this(null, null);
}
public SLLNode(T el){
this(el, null);
}
public SLLNode(T el, SLLNode<T> ptr){
info = el;
next = ptr;
}
}
Flight
類:
public class Flight {
int id;
String flightHour;
TicketList tickets = new TicketList();
public Flight(){
this.id = 0;
this.flightHour = "";
this.tickets = null;
}
@Override
public boolean equals(Object obj){
Flight s = (Flight)obj;
return this.id == s.id;
}
}
listFlight
類:
public class Flights extends SLL<Flight>{
public void reserve(Flight f){
if(check(f) == false)
this.addToTail(f);
}
public void cancel(Flight f){
this.delete(f);
}
public boolean check(Flight f){
return this.isInList(f);
}
public void display(){
if(!isEmpty())
this.printAll();
}
}
SLL
類:
public class SLL<T> {
protected SLLNode<T> head, tail;
public SLL(){
head = tail = null;
}
public boolean isEmpty(){
return head == null;
}
public boolean isInList(T el){
SLLNode<T> tmp;
for(tmp = head; tmp != null && !tmp.info.equals(el); tmp = tmp.next);
return tmp != null;
}
public T get(T el){
SLLNode<T> tmp;
for(tmp = head; tmp != null && tmp.info.equals(el); tmp = tmp.next);
return (T)tmp;
}
}
我沒有看到listFlight'的'定義在你的代碼。 – MaxZoom
listFlight class: http://notepad.cc/xoggajo56 SLL class:http://notepad.cc/xaslaucu47 –
'tmp'是一個'SLLNode'。不'SLLNode'有一個方法來檢索它的有效載荷,'T'? –