简体   繁体   English

扩展Java的链表供个人使用

[英]extending Java's Linked List for personal use

So I need a linked list to store multiple variables, so I want to use my hand-made Linked List ADT, but also want Java's Linked List's Comparator Sort from collections.sort(); 因此,我需要一个链表来存储多个变量,因此我想使用手工制作的链表ADT,还希望Java的链表的Comparator Sorts来自collections.sort();。

So I tried and edited my code as following: 所以我尝试并编辑了我的代码,如下所示:

public class HLinkedList <HTreeNode>  extends LinkedList <HTreeNode>
{

public class HTreeNode {


    public HTreeNode left;
    public HTreeNode right;
    public HTreeNode next;
    public int frequency;
    public char value;
    public String code;
    public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
    {
        value = val;
        frequency = freq;
        left = l;
        right = r;
        next = n;
        code = ""; // just initialized ,but have to think through logic.
    }
}

but if I do this, instead of just public class HLinkedList (which is fine except I can't use Collections.sort(HList, comparatorA) , one line of code that I need to have Java's Linked List for. 但是,如果我这样做,则不仅仅是public class HLinkedList (这很好,但我不能使用Collections.sort(HList, comparatorA) ,这是我需要具有Java的链表的一行代码。

anyways, if I have my code as shown above it returns a 无论如何,如果我的代码如上所示,它将返回一个

cannot make static reference to non-static type HTreeNode in following line, 

with red lining under HTreeNode. 在HTreeNode下有红色衬里。 This does not happen if I do not try to extend LinkedList. 如果我不尝试扩展LinkedList,则不会发生这种情况。

public static void insertIntoPosition(HTreeNode node, int position)

also following the above error is an error in 也跟随上述错误是在

public HLinkedList() //constructor
{
    head = null; //inital value
    nItem = 0;//counter

}

where multiple occurences in the code indicate they cannot static reference a non-static head. 代码中多次出现表示它们不能静态引用非静态头。 Usually when stuff like this comes up I click "make that head static", but in this case when I do that even more errors pop up, now pointing to ALL the "head" references. 通常,当出现类似这样的问题时,我单击“使该头变为静态”,但是在这种情况下,我弹出了更多错误,现在指向所有“头”引用。

I would just like to know what is going on when I am trying and failing at extending LinkedList, or if I'm supposed to not extend but do something else maybe? 我只想知道在尝试扩展LinkedList失败时发生了什么,或者是否应该扩展而不是做其他事情?

The following method is a class method : 以下方法是一个类方法

 public static void insertIntoPosition(HTreeNode node, int position)

Thus it does not need an instance of HLinkedList in order to be invoked. 因此,它不需要HLinkedList实例即可被调用。

However, your class HTreeNode is an inner class - and need an "attahced" instance of HLinkedList in order to be instantiated. 但是,您的类HTreeNode是一个内部类 -需要实例化HLinkedList的“附加”实例。

These two facts contradict each other (you cannot instantiate an object of this type in this method) 这两个事实相互矛盾(您无法在此方法中实例化此类型的对象)

You can add the static keyword to the declaration of HTreeNode in order to overcome this: 您可以将static关键字添加到HTreeNode的声明中,以克服此问题:

public static class HTreeNode { //note the static keyword usage
    ....
}

Another problem is that you should not have a generic type qualifier on HLinkedList . 另一个问题是,您不应在HLinkedList上具有泛型类型限定符。 It's not doing what you think it is. 它没有按照您的想法做。 What you are specifying is to create a class that stores "something", and this isn't the inner class. 您要指定的是创建一个存储“内容”的类,这不是内部类。 Instead, you need to move the HTreeNode class outside HLinkedList and then use this instead: 相反,你需要移动HTreeNode类外HLinkedList ,然后用这个来代替:

public class HLinkedList extends LinkedList <HTreeNode>

Otherwise, the generic type hides the inner class and I can also create a HLinkedList that stores strings. 否则,泛型类型将隐藏内部类,我还可以创建一个存储字符串的HLinkedList

HLinkedList<String> hll = new HLinkedList<String>();
hll.add("Hello World");

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

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