简体   繁体   中英

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

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.

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. This does not happen if I do not try to extend 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?

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.

However, your class HTreeNode is an inner class - and need an "attahced" instance of HLinkedList in order to be instantiated.

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:

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

Another problem is that you should not have a generic type qualifier on 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:

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<String> hll = new HLinkedList<String>();
hll.add("Hello World");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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