gpt4 book ai didi

java - 频率包 - getFrequencyOf(aData)

转载 作者:行者123 更新时间:2023-11-30 07:36:26 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com