The below is an excerpt from a file in a project creating Huffman Trees. When the
“public class HLinkedList” is initialized by itself, the separate file (also shown below) of HTreeNode ‘s methods such as .next, .left, .right have no problem working in HLinkedList’s file. Once the “extends java.util.Abstract~” is put in place, the HTreeNode methods cannot be referenced, and I resorted to copy-pasting HTreeNode into the same file, which is why the first lines of code show it as such. The thing with having both of these in the same file is that it’s causing collision(I think) between HLinkedList.HTreeNode and HTreeNode, as I kept the HTreeNode file just in case. This happens in a separate file Huffman_coder which originally used both HLinkedList and HTreeNode files very well (or at least without compile level error) until I decided to extend java.util.Abstract~
public class HLinkedList<HTreeNode> extends java.util.AbstractSequentialList<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.
}
}
HTreeNode head;
static int nItem;
public HLinkedList() //constructor
{
head = null; //inital value
nItem = 0;//counter
}
public void insertIntoPosition(HTreeNode node, int position) //inserts into position
{
//first, the case where it's already in the list.
HTreeNode currNode = head;
while(currNode.next != null)
{
if(currNode.value == node.value)
{
currNode.frequency++;
}
currNode = currNode.next;
}
if(currNode.value == node.value)
{
currNode.frequency++;
}
HTreeNode file:
public class HTreeNode {
public static 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.
}
}
If you want to extend then you should extend
java.util.AbstractSequentialList<E>, the generic version instead of the raw one.Your compiler was complaining because either you didn’t import the class, or your methods were not public, or you are not invoking them the right way (may you made them non-static and tried to invoke on the class itself).
Update
I am not sure of what you are trying to achieve, but believe is something that doesn’t generate any compiler error (with some comments) –
Now imagine, the following is how the clients will be using
HLinkedList–