java - 频率包 - getFrequencyOf(aData)

标签 java data-structures linked-list

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;

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

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

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

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

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

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

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

关于java - 频率包 - getFrequencyOf(aData),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35331100/

相关文章:

java.io.IOException : read past EOF Lucene in Java with Eclipse

java - 从 Java 中的 HashMap 获取 key

java - 如何为我的图表创建 addge 函数。我需要将边添加到节点

java - 在字符串集中搜索字符串排列

java - 从链表中删除一个元素

c++ - 返回指向链表类中结构的指针

java - Glassfish - 从 32 位到 64 位操作系统的 Java EE 应用程序,不完整的数据库查询?

java - 了解 Netbeans GUI 生成的代码中的按钮/鼠标监听器

java - 如何使用 selenium webdriver 在日期选择器中选择开始和结束日期?

c++ - 它用什么样的二叉搜索树来实现 `std::set` ?