链表中的 getFrequencyOf() 字符串

getFrequencyOf() strings in linked list

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

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

你好你好吗我找到了谢谢你我很好谢谢你

我的 size()getFrequencyOf("am") 方法工作正常。但是,我的 getMaxFrequency() returns 2,而它应该是 return 4(因为 "you" 出现了 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
}
}

问题是 max 的值不会改变,除非你先调用 getFrequencyOf("you")

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