Frequency Bag - getFrequencyOf(aData)



getFrequencyOf(aData)方法遇到问题,为我在测试代码中搜索的每个元素返回零...这是我的代码,如果有人可以指出我做错了什么,将不胜感激

public class FrequencyBag<T>
{
    // TO DO: Instance Variables
    private Node firstNode;
    private int numberOfEntries;
    /**
     * Constructor
     * Constructs an empty frequency bag.
     */
    public FrequencyBag()
    {
        // TO DO
        firstNode = null;
        numberOfEntries = 0;
    }
    /**
     * Adds new entry into this frequency bag.
     * @param aData the data to be added into this frequency bag.
     */
    public void add(T aData)
    {
        // TO DO
        if(numberOfEntries != 0){
            Node newNode = new Node(aData);
            newNode.next = firstNode.next;
            firstNode = newNode;
            numberOfEntries++;
        }
        else{
            Node newNode = new Node(aData);
            firstNode = newNode;
        }
    }
    /**
     * Gets the number of occurrences of aData in this frequency bag.
     * @param aData the data to be checked for its number of occurrences.
     * @return the number of occurrences of aData in this frequency bag.
     */
    public int getFrequencyOf(T aData)
    {
        // TO DO
        int counter = 0;
        Node currentNode = firstNode; 
        for(int i = 1; i <= numberOfEntries; i++)
        {
            if(currentNode.data.equals(aData))
            {
                counter++;
                System.out.println(counter);
            }
            currentNode = currentNode.next;
        }
        System.out.println(counter);
        return counter;
    }
    /**
     * Gets the maximum number of occurrences in this frequency bag.
     * @return the maximum number of occurrences of an entry in this
     * frequency bag.
     */
    public int getMaxFrequency()
    {
        // TO DO
        Node currentNode = firstNode;
        int currentFrequency = 0;
        int maxFrequency = currentFrequency;
        for(int i = 1; i <= numberOfEntries; i++)
        {
            currentFrequency = getFrequencyOf(currentNode.data);
            if(currentFrequency > maxFrequency)
            {
                maxFrequency = currentFrequency;
            }
            currentNode = currentNode.next;
        }
        return maxFrequency;
    }
    /**
     * Gets the probability of aData
     * @param aData the specific data to get its probability.
     * @return the probability of aData
     */
    public double getProbabilityOf(T aData)
    {
        // TO DO
        int num = getFrequencyOf(aData);
        double probability = num / numberOfEntries;
        return probability;
    }
    /**
     * Empty this bag.
     */
    public void clear()
    {
        // TO DO
        firstNode.next = null;
        firstNode.data = null;
    }
    /**
     * Gets the number of entries in this bag.
     * @return the number of entries in this bag.
     */
    public int size()
    {
        // TO DO
        return numberOfEntries;
    }

    private class Node
    {
        private T data;
        private Node next;
        public Node (T a)
        {
            data = a;
            next = null;
        }
        public Node(T a, Node n)
        {
            data = a;
            next = n;
        }
    }

}

老实说,我没有阅读完整的代码,因为add方法中存在错误,这可能会导致您面临的问题:

Node newNode = new Node(aData);
newNode.next = firstNode.next; //firstNode.next is null!
firstNode = newNode;

添加第一个节点时,它将指向一个空节点(其下一个值为 null)。

然后,当您添加第二个节点时,如上行所示,列表的新头部指向前一个头部的下一个节点,该节点始终为 null。因此,您的列表始终只有一个节点,即 firstNode .要解决此问题,请将上述行更改为:

Node newNode = new Node(aData);
newNode.next = firstNode;
firstNode = newNode;

或(使用第二个构造函数):

Node newNode = new Node(aData,firstNode);
firstNode = newNode;

这将使您的新节点成为链表的头部,其下一个元素是列表的前一个头部。

更新:此外,另一个问题是numberOfEntries始终为 0,因为在添加第一个节点时不会递增它。因此,在 add() 方法的 else 块内添加一个numberOfEntries++;,或者只是将if块中存在的移动到块之外。

相关内容

  • 没有找到相关文章

最新更新