2015-05-27 48 views
-1
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; 
    } 
} 
+0

我没有看到listFlight'的'定义在你的代码。 – MaxZoom

+0

listFlight class: http://notepad.cc/xoggajo56 SLL class:http://notepad.cc/xaslaucu47 –

+0

'tmp'是一个'SLLNode '。不'SLLNode'有一个方法来检索它的有效载荷,'T'? –

回答

1

返回的SLLNodeinfo成员应修复异常:

public T get(T el){ 
    SLLNode<T> tmp; 
    for(tmp = head; tmp != null && tmp.info.equals(el); tmp = tmp.next); 

    return tmp.info; // Changed line 
} 
+0

谢谢您的建议:d 我解决了问题:D –

1

equals功能不符合equals合同是一致的:它不应该抛出Exception,但你扔在obj不是Flight的情况下的例外,你应该先检查是否objFlight

@Override 
public boolean equals(Object obj){ 
    if (!(obj instanceof Flight)) return false; 
    // or even 
    // obj.getClass().equals(Flight.class) 
    // if you want exactly this class and not its descendants 

    Flight s = (Flight)obj; 
    return this.id == s.id; 
} 

所以,你的错误看起来像你已经通过了一些东西,那不是FlightFlight.equals

+0

我同样的功能运作良好。的 listFlight.get(FF2) 值是一个飞行 和TMP也飞行 ,但我不能用TMP = listFlight.get(FF2) –

+0

@TrungThành显然,这是'fullversion.SLLNode' –