2015-02-03 3 views
0

내 코드에 이상한 문제가 있습니다. 정수로 테스트 할 때, 모든 것이 완벽하게 작동합니다. 1,000,000 개의 데이터도 있습니다. 나는 그것을 지우고 새로운 데이터를 입력 할 수 있으며 각 메소드는 올바른 값을 반환 할 것이다. 그러나 일단 String을 사용하여 주파수 대역을 구성하면 문제가 발생합니다. 감사합니다 그리고 난 괜찮아요 당신에게 내 size() getFrequencyOf() 연결된 목록의 문자열

감사 및 getFrequencyOf("am") 방법이 제대로 작동을 얼마나

안녕하세요 제가 찾을 수 있어요 : 나는 데이터 문자열을 추가 할 때

특히,. 그러나 내 getMaxFrequency()는 2를 반환하지만 4는 반환해야합니다 ("you"가 4 번 표시되기 때문에).

처음에는 "2"가 "am"이 나타나는 횟수이기 때문에 생각했지만 최대 마진이 아닌 변수 마비 만 재설정 했으므로 왜 이렇게하는지 이해할 수 없습니다. 왜 그것은 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 
} 
} 

답변

2

getFrequencyOf("you")을 먼저 호출해야 최대 값을 변경할 수 있습니다.

getMaxFrequency 함수에서 다른 루프를 추가하고 linkedlist의 각 노드에 getFrequencyOf()을 호출 한 다음 max를 반환하십시오. 그것은 잘 작동합니다.

+0

지금은 분명해 보입니다 ... 감사합니다! 빠르고 쉬운 수정 (7 분 안에 답을 수락). –

+0

도움이 될 수있어서 기쁩니다. :-) – vipluv