因此,我正在最后确定程序,在该程序中,测试将通过测试程序中的单词列表并使用前缀,我对其进行除草,仅返回匹配的单词。
这是我的contains方法和测试方法摘要的以下代码,我只是想知道我做错了什么?
//包含方法
public boolean containsKey(TrieMapNode current, String curKey) {
// recursively get the value for the current node
String value = get(current,curKey);
// if value if null or empty, key is not found return false
if(value == null) {
return false;
}else if (value.equals("")) {
return false;
} else {
return true;
}
}
//测试程序
import java.util.HashSet;
import java.util.ArrayList;
public class TriMapTester{
public static void main(String[] args){
HashSet<String> posWords = getPositiveWords();
HashSet<String> negWords = getNegativeWords();
TrieMapInterface map = new TrieMap();
//Add all of the positive words
for(String word: posWords){
map.put(word, word);
}
int countErrors = 0;
int totalErrors = 0;
//Check that the map contains all positive words
for(String s: posWords){
if(!map.containsKey(s)){
countErrors++;
}
}
System.out.println("Number of missed keys after searching all positive words: " + countErrors);
totalErrors += countErrors;
countErrors = 0;
//Check that the correct value is associated with each key
for(String s: posWords){
if(!map.containsKey(s) || !map.get(s).equals(s)){
countErrors++;
}
}
System.out.println("Number of incorrect values for keys: " + countErrors);
totalErrors += countErrors;
countErrors = 0;
//Check negative words. Make sure they do not show up in map if they are not also in positive words.
for(String s: negWords){
if(map.containsKey(s) && !posWords.contains(s)){
countErrors++;
}
}
System.out.println("Number of incorrectly found keys after searching all negative words: " + countErrors);
totalErrors += countErrors;
//Check the values returned for a number of prefix values
//Compares the returned result to the true result
String[] searchPrefixes = {"add", "bril", "cat", "cri", "derri", "mar", "tra", "lor", "marveled", "rit"};
for(String prefix: searchPrefixes){
System.out.println("Getting values for prefix: " + prefix);
ArrayList<String> realResult = matchPrefix(prefix, posWords);
ArrayList<String> yourResult = map.getValuesForPrefix(prefix);
System.out.println("Result should contain: " + realResult);
System.out.println(" Your map returned: " + yourResult);
countErrors = countDifference(realResult, yourResult);
totalErrors += countErrors;
System.out.println("Error Count: " + countErrors);
}
System.out.println("Total Errors: " + totalErrors);
}
public static int countDifference(ArrayList<String> realResult, ArrayList<String> yourResult){
int result = 0;
for(String s: realResult){
if(!yourResult.contains(s)){
result++;
}
}
for(Object s: yourResult){
if(!realResult.contains((String)s)){
result++;
}
}
return result;
}
public static ArrayList<String> matchPrefix(String prefix, HashSet<String> words){
ArrayList<String> result = new ArrayList<String>();
for(String s: words){
if(s.startsWith(prefix)){
result.add(s);
}
}
return result;
}
public static HashSet<String> getPositiveWords(){
HashSet<String> result = new HashSet<String>();
String[] pos = new String[]{"abound", "abounds", "abundance", "abundant", "accessable", "accessible", "acclaim", "acclaimed", "acclamation"};
for(String s: pos){
result.add(s);
}
return result;
}
public static HashSet<String> getNegativeWords(){
HashSet<String> result = new HashSet<String>();
String[] neg = new String[]{"abnormal", "abolish", "abominable", "abominably", "abominate", "abomination", "abort", "aborted", "aborts", "abrade", "abrasive", "abrupt", "abruptly", "abscond", "absence", "absent-minded"};
for(String s: neg){
result.add(s);
}
return result;
}
现在,运行测试时得到的输出是正确的包含单词,但是尽管包含contains方法,错误的数量仍然为0,所以我不确定我在做什么错。任何帮助将不胜感激
答案 0 :(得分:0)
问题在这一行
if(map.containsKey(s) && !posWords.contains(s))
countErrors++;
map&posWords包含相同的数据,您一次要检查它是否为真,还要检查它是否为假,这将永远不会执行countErrors++
。这就是为什么countErrors为0的原因。