链表中的getFrequencyOf()字符串

时间:2015-02-03 20:16:30

标签: java string linked-list

我的代码遇到了一个奇怪的问题。当我用整数测试它时,一切都很完美,即使有1,000,000个数据。我可以清除它并输入新数据,每个方法都会返回正确的值。但是,一旦我使用String构建和频率包,我就遇到了问题。

具体来说,当我添加数据字符串时:

你好,我怎么找到你,谢谢你,我很好,谢谢你

我的size()getFrequencyOf("am")方法正常运行。但是,我的getMaxFrequency()返回2,而它应该返回4(因为“你”出现4次)。

起初我以为是因为“2”是“am”出现的次数,但我只重置变量numb,而不是max,所以我无法理解为什么会这样做。更不用说为什么它与int完美配合,而不是String。

public class FrequencyBag<T>
{
private class Node                                                      // Node class
{
    private T data;                                                     // Initialize data variable
    private Node next;                                                  // Create Node next
    private Node(T aData, Node nextNode)                                            // Create Node (link data to next Node)
    {
        data = aData;                                                   // Set data to aData
        next = nextNode;                                                // set next to nextNode
    }

    private Node(T aData)                                                   // Create Node (aData)
    {
        this(aData, null);                                              // Link this to Node
    }
}                                                               

//-----------------------------------------------// TO DO: Instance Variables

private Node firstNode;                                                     // Initialize firstNode
private int numberOfEntries;                                                    // Initialize numberOfEntries
private int numb;                                                       // Initialize numb (occurrences)
private int max = 0;                                                        // Initialize max

/**
 * Constructor
 * Constructs an empty frequency bag.
 */

public FrequencyBag()
{
    //---------------------------------------// TO DO:

    firstNode = null;                                                   // Construct empty bag
    numberOfEntries = 0;                                                    // Set numberOfEntries to 0 (empty)
}

/**
 * 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:

    Node temp = firstNode;                                                  // Set first node to temp
    firstNode = new Node(aData, temp);                                          // Add new node to beginning (link to temp)
    numberOfEntries++;                                                  // Incriment numberOfEntries
}

/**
 * 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:

    numb = 0;                                                       // Reset numb (occurrences)
    Node currentNode = firstNode;                                               // Create currentNode/set to firstNode

    while(currentNode != null)                                              // While the list exists/continues...
    {
        if(currentNode.data.equals(aData))                                      // If the current node equals aData
        {
            numb++;                                                 // Incriment numb (occurrences)
        }

    currentNode = currentNode.next;                                             // Set current node to next list item
    }

    if(numb > max)                                                      // If numb (occurrences) > max...
    {
        max = numb;                                                 // Set new max
    }

    return numb;                                                        // Return numb (occurrences)
}

/**
 * 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:

    return max;                                                     // Return max (set in getFrequencyOf()
}

/**
 * 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:

    numb = getFrequencyOf(aData);                                               // Find current numb (occurrences)
    double probb = (numb / (double)numberOfEntries);                                    // Set probb to probability
    return probb;                                                       // Return probb
}

/**
 * Empty this bag.
 */

public void clear()
{
    //---------------------------------------// TO DO:

    for(int i = 0; i < numberOfEntries; i++)                                        // For each node...
    {
        firstNode = firstNode.next;                                         // Remove the first node
    }

    numberOfEntries = 0;                                                    // Reset numberOfEntries
    max = 0;                                                        // Reset max
}

/**
 * Gets the number of entries in this bag.
 * @return the number of entries in this bag.
 */

public int size()
{
    //---------------------------------------// TO DO:

    return numberOfEntries;                                                 // Return numberOfEntries
}
}

1 个答案:

答案 0 :(得分:2)

问题是,除非您先拨打getFrequencyOf("you"),否则max的值不会发生变化。

getMaxFrequency函数中,添加另一个循环并为链表中的每个节点调用getFrequencyOf(),然后返回最大值。它会正常工作。