我不知道在哪里或如何放置我的 return 语句,所以我返回一个链表。链表的返回是循环的。但是,我需要在此之外有一个退货声明,但无法正常工作。我只是不断得到空返回。尝试放入 else 语句,但它不起作用。我觉得这是一个简单的解决方案,但无法解决。我知道在这段代码中总是读取返回 null,只是不知道如何放置它,所以它并不总是返回 null。
方法是接收一串数字,将字母附加到键盘上的这些数字上,并将这些字符串的可能组合放入查找单词并返回这些单词的链接列表的方法中。
只是无法计算出方法中的return语句。
public LinkedList bfs_search(String numberEntered) {
//use a queue for all the unsearched combinations to go
Queue<String> q = new LinkedList<String>();
q.add("");
String possibleWord = null;
//loop through each number entered
for(int i = 0; i < numberEntered.length(); i++) {
//get the letters on the keypad for current number
String lettersOnKeypadNumber = T9keypad[numberEntered.charAt(i) - ASCII_ZERO];
int len = q.size();
//loop while there are letter sequences in the queue. Note len-- is same as len=len-1 so loop until only one item left in queue (the blank item inserted above)
while(len -- > 0) {
String letterSequenceFromQ = q.remove();
//loop through each letter on the keypad number, add it to the letter sequence pulled from queue and search for that letter sequence as a word in the trie
for(int j = 0; j < lettersOnKeypadNumber.length(); j++) {
possibleWord = letterSequenceFromQ + lettersOnKeypadNumber.charAt(j);
//q.add(tmpStr);
if(search(possibleWord) && possibleWord.length() == numberEntered.length()) {
//found it!
return possibleWords(possibleWord);
} else {
//letter sequence is not a word, add it to the queue for possible word when get the next set of letters for the next keyed number
q.add(possibleWord);
}
}
}
}
return null;
}