链接列表的返回值?

时间:2015-05-27 14:26:59

标签: java

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类和get函数:

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

显示错误:

  

线程“main”中的异常java.lang.ClassCastException:fullversion.SLLNode无法强制转换为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;
    }
}

2 个答案:

答案 0 :(得分:1)

您的equals函数与equals契约不一致:它永远不会抛出Exception,但如果obj不是{{1},则抛出异常首先,您应该检查Flight是否为obj

Flight

因此,您的错误似乎已经传递了一些内容,而不是@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; } Flight

答案 1 :(得分:1)

返回info SLLNode成员应该修复例外:

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
}