我希望输出将是在forList退出之前与搜索匹配的wordList中的字符串,但是当if语句满足条件时,它不会每次输出。
search =“ ABC”
wordList = [[“ ABC”,“ 123”],[“ ABC”,“ 456”],[“ DEF”,“ 123”]]
public void biDi(String searchWord, String[][] wordList) {
int start = 0;
int end = list.size ()-1;
String search = searchWord;
int path = 0;
for (int i = 0; (i < (list.size ()/2)); i++) {
if (search == wordList[start][0]) {
System.out.println (wordList[start][1]);
}
if (search == wordList[end][0]) {
System.out.println (wordList[end][1]);
}
start++;
end--;
path++;
}
System.out.println (path);
}
答案 0 :(得分:1)
您需要使用equals
而不是==
,在字符串比较参考而非值上使用==
。
if (search.equals(wordList[start][0])) {
System.out.println(wordList[start][1]);
}
if (search.equals(wordList[end][0])) {
System.out.println(wordList[end][1]);
}