简体   繁体   English

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

[英]Overriding the compareTo method with a class that is nested inside another class which extends Comparable

I am tasked with writing my own PriorityQueue class in Java. 我的任务是用Java编写自己的PriorityQueue类。 It is based on LinkedLists. 它基于LinkedLists。 To quote the directions: 引用说明:

The type of the data stored in the nodes should be a generic type that is comparable. 节点中存储的数据类型应为可比较的通用类型。 That is write for the class declaration: public class PriorityQueue (E extends Comparable)) -> note: the curly braces are meant to mean <>, whatever I write between <> is disappearing... 那是为类声明写的:公共类PriorityQueue(E扩展为Comparable))->注意:花括号的意思是<>,我在<>之间写的任何内容都消失了...

I will be using the PriorityQueue to write two other classes, one of type patient, the other of waitingRoom. 我将使用PriorityQueue编写其他两个类,一个类为Patient类型,另一个类为waitingRoom。 This is where the compareTo method will come into play, as I sort the two classes into their individual PriorityQueues. 这是compareTo方法发挥作用的地方,因为我将两个类排序到各自的PriorityQueues中。

I have been defining the ListNode class inside of the PriorityQueue class itself, so I have a class within a class. 我一直在PriorityQueue类本身内部定义ListNode类,因此我在一个类中有一个类。 Now comes the question: 现在出现问题:

Where am I going to implement/Override the inherited compareTo method from Comparable? 我要在哪里实现/重写Comparable继承的compareTo方法?

It can't get implemented in the PriorityQueue class because compareTo can only take one argument. 它不能在PriorityQueue类中实现,因为compareTo只能接受一个参数。 Yet, this is where it seems like it should go, as this is the actual class extending Comparable. 然而,这似乎是应该去的地方,因为这是扩展Comparable的实际类。

If I implement it inside the ListNode class, well, I have no idea how I would. 如果我在ListNode类中实现它,那我将不知道该怎么做。 Do I turn ListNode into an interface? 我是否可以将ListNode转换为接口? An AbstractClass? 一个AbstractClass?

Below is the quite novice code I have written, thanks for the help 以下是我编写的相当新手的代码,感谢您的帮助

    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);
        }

    }







}

If you are wanting to compare two ListNodes, as in the line: 如果要比较两个ListNodes,如下所示:

n1.compareTo(n)

You'd need to make it implement Comparable: 您需要使其实现Comparable:

public class ListNode implements Comparable<ListNode> {

And implement the compareTo method as something like: 并实现compareTo方法,例如:

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

(since you know the data is Comparable ). (因为您知道dataComparable )。 But you would have to handle the case of null data. 但是您将不得不处理空数据的情况。


Note that you should also declare your type variable on the top-level class as: 请注意,您还应该在顶级类上将类型变量声明为:

<E extends Comparable<? super E>>

Each class that you will use as element type of PriorityQueue must implement the Comparable interface and the compareTo method. 您将用作PriorityQueue元素类型的每个类都必须实现Comparable接口和compareTo方法。

Note that as your ListNode class implements a compareTo method, you could have made it implement Comparable<ListNode> : 请注意,由于ListNode类实现了compareTo方法,因此可以使它实现Comparable<ListNode>

public class ListNode implements Comparable<ListNode>  {

... ...

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

Note that as you ListNode classe doesn't depend on an instance of PriorityQueue , you could have made it static ; 请注意,由于ListNode不依赖于PriorityQueue实例,因此可以将其设置为static in which case, you would have had to declare a generic argument: 在这种情况下,您将不得不声明一个通用参数:

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);
    }
}

class ListNode has fields, so you can't change it to an interface (why the hell would you do that anyway?) ListNode类具有字段,因此您不能将其更改为接口(为什么还要这么做呢?)

You should perhaps ask yourself if you really need a doubly linked list and if a singly linked list would not suffice. 您也许应该问问自己,您是否真的需要一个双向链表,以及一个单链表是否足够。

It is unclear in your question if you need to implement a linked list or if you could make use of the class LinkedList in which case you would not need the ListNode class: PriorityQueue would just encapsulate a LinkedList<E> . 在您的问题中不清楚是需要实现链接列表还是可以使用LinkedList类,在这种情况下,您将不需要ListNode类: PriorityQueue只会封装LinkedList<E>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM