1

以下是创建 Huffman 树的项目中文件的摘录。当“公共类HLinkedList”自行初始化时,HTreeNode的.next、.left、.right等方法的单独文件(也如下所示)在HLinkedList的文件中工作没有问题。一旦“extends java.util.Abstract~”放置到位,HTreeNode 方法就不能被引用,我不得不将 HTreeNode 复制粘贴到同一个文件中,这就是第一行代码显示它的原因。将这两者放在同一个文件中的问题是它会导致 HLinkedList.HTreeNode 和 HTreeNode 之间发生冲突(我认为),因为我保留了 HTreeNode 文件以防万一。

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 文件:

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.
    }
}
4

1 回答 1

1

如果你想扩展java.util.AbstractSequentialList<E>,那么你应该扩展通用版本而不是原始版本。

您的编译器抱怨是因为您没有导入类,或者您的方法不是公共的,或者您没有以正确的方式调用它们(可能您将它们设为非静态并尝试在类本身上调用)。

更新


我不确定您要达到的目标,但相信不会产生任何编译器错误(带有一些评论)-

//E is a type parameter, meaning HLinkedList<E> is generic
public class HLinkedList<E> extends java.util.AbstractSequentialList<E> {

    //I think you can keep this private here, this only helps you to implement your
    //version of AbstractSequentialList, for anyone who will be using HLinkedList
    //are not going to worry about it
    private static 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)  {
            value = val;
            frequency = freq;
            left = l;
            right = r;
            next = n;
            code = ""; 
        }
    }

    private HTreeNode head;
    private int nItem; //made this non-static, each instance will need it's own copy

    public HLinkedList()  {
        this.head = null;
        this.nItem = 0;
    }

    public void insertIntoPosition(E element, int position)  {
        // probably create a new node here for element
        // and fix it at the location specified
    }

    //This is an abstract method declared in AbstractSequentialList
    //You need provide an implementation of it
    @Override
    public ListIterator<E> listIterator(int index) {

        return null;
    }

    //This is an abstract method declared in AbstractSequentialList
    //You need provide an implementation of it    
    @Override
    public int size() {

        return 0;
    }
}

现在想象一下,以下是客户将如何使用HLinkedList-

HLinkedList<Integer> list = new HLinkedList<Integer>();
//or
HLinkedList<String> list = new HLinkedList<String>();
于 2012-10-19T00:51:55.077 回答