用嵌套在另一个扩展Comparable的类中的类覆盖compareTo方法

时间:2018-07-27 04:39:23

标签: java generics comparable

我的任务是用Java编写自己的PriorityQueue类。它基于LinkedLists。引用说明:

存储在节点中的数据类型应为可比较的通用类型。那是为类声明写的:公共类PriorityQueue(E扩展为Comparable))->注意:花括号的意思是<>,我在<>之间写的东西都消失了……

我将使用PriorityQueue编写其他两个类,一个类为Patient类型,另一个类为waitingRoom。当我将两个类排序到各自的PriorityQueues中时,compareTo方法就会发挥作用。

我一直在PriorityQueue类本身内定义ListNode类,因此我在一个类中有一个类。现在出现了一个问题:

我要在哪里实现/重写Comparable继承的compareTo方法?

它不能在PriorityQueue类中实现,因为compareTo只能接受一个参数。但是,这似乎是应该去的地方,因为这是扩展Comparable的实际类。

如果我在ListNode类中实现它,那我不知道该怎么做。我是否可以将ListNode转换为接口?一个AbstractClass?

下面是我写的新手代码,感谢您的帮助

    package hostpitalQueue;

import java.util.AbstractList;

public class PriorityQueue<E extends Comparable<E>>  {

    private ListNode front;

    public PriorityQueue() {
        front = null;
    }
    public PriorityQueue(ListNode n1) {
        front = n1;
    }



    //method for addingNode to beginning, 
    //perhaps overload method for next nodes?
    public void addNode(ListNode n1) {

        if(front == null) {
            front = n1;
        }else {
                //need to find last node and add n1 to it
                ListNode lastNode = findLastNode(n1);
                lastNode.addNode(n1);
            }
    }



    //need to compare, remember, this is a priorityqueue
    public ListNode findLastNode(ListNode n) {
        //compare the data of both
        //compare to front

        ListNode n1 = front;
        int i = n1.compareTo(n);
        //only do something here if n is higher priority
        if(i > 0) {
            E frontData = n1.data;
            E nodesData = n.data;
            ListNode holder = n1;
            front = n;
            n.next = holder;
            holder.previous = n;
        }else if(n1.next == null) {
            n1.next = n;
            n.previous = n1;
        }
        else {

            while(front.next != null) {
                n1 = front.next;
                //is n1 a higher priority?
                Integer ii = n1.compareTo(n);
                if(ii > 0) {
                    //this means that we should return the previous node, to insert
                    //before this one
                    return n1.previous;
                }
            }
        }
        return n1;
    }


    public class ListNode  {
        //contains a left and a right, as well as a data field
        public E data;
        public ListNode previous,next;


        //construct
        public ListNode() {
            data = null;
            previous = null;
            next = null;
        }

        //previous to next
        public ListNode(E data) {
            this.data = data;
            previous = null;
            next = null;
        }

        public ListNode(E data,ListNode n1) {
            this.data = data;
            previous = n1;
            next = null;
        }

        public ListNode(E data,ListNode n1,ListNode n2) {
            this.data = data;
            previous = n1;
            next = n2;
        }

        public void addNode(ListNode n1) {
            //gotta check if my next is null
            ListNode holder = null;
            if(this.next != null) {
                holder = this.next;
            }
            this.next = n1;
            n1.previous = this;
            n1.next = holder;
        }
        public int compareTo(ListNode n1) {
            return 0;
        }

        public void printMe() {
            System.out.println(this.data);
        }

    }







}

2 个答案:

答案 0 :(得分:0)

如果要比较两个ListNode,如下所示:

n1.compareTo(n)

您需要使其实现Comparable:

public class ListNode implements Comparable<ListNode> {

并以类似以下方式实现compareTo方法:

return this.data.compareTo(that.data);

(因为您知道dataComparable)。但是您将不得不处理空数据的情况。


请注意,您还应该在顶级类上将类型变量声明为:

<E extends Comparable<? super E>>

答案 1 :(得分:0)

您将用作PriorityQueue元素类型的每个类都必须实现Comparable接口和compareTo方法。

请注意,当您的ListNode类实现compareTo方法时,您可能已经使其实现了Comparable<ListNode>

public class ListNode implements Comparable<ListNode>  {

...

    @Override
    public int compareTo(ListNode n1) {
        return data.compareTo(n1.data);
    }

请注意,由于您ListNode的类别不依赖于PriorityQueue的实例,因此可以将其设为static;在这种情况下,您将必须声明一个通用参数:

public static class ListNode<T extends Comparable<T>>
        implements Comparable<ListNode<T>>  {
    //contains a left and a right, as well as a data field
    public T data;
    public ListNode<T> previous,next;


    //construct
    public ListNode() {
        data = null;
        previous = null;
        next = null;
    }

    //previous to next
    public ListNode(T data) {
        this.data = data;
        previous = null;
        next = null;
    }

    public ListNode(T data,ListNode<T> n1) {
        this.data = data;
        previous = n1;
        next = null;
    }

    public ListNode(T data,ListNode<T> n1,ListNode<T> n2) {
        this.data = data;
        previous = n1;
        next = n2;
    }

    public void addNode(ListNode<T> n1) {
        //gotta check if my next is null
        ListNode<T> holder = null;
        if(this.next != null) {
            holder = this.next;
        }
        this.next = n1;
        n1.previous = this;
        n1.next = holder;
    }

    @Override
    public int compareTo(ListNode<T> n1) {
        return data.compareTo(n1.data);
    }

    public void printMe() {
        System.out.println(this.data);
    }
}

ListNode有字段,所以您不能将其更改为接口(为什么还要这么做呢?)

您也许应该问自己,您是否真的需要一个双向链表,单链表是否足够。

您的问题尚不清楚,您是否需要实现链接列表,或者是否可以使用类LinkedList,在这种情况下,您将不需要ListNode类:{{1} }只会封装一个PriorityQueue