public class Main { public static void main(String[] args) { String[] find2 = {"cool","NOT","NOT,"Cool"}; String[] nofind = {"Fly","poke","ok"; System.out.println(Test.out(find); System.out.println(Test.out(nofind)); } }+public class Test {public static boolean Out(String[] input) { for (int i = 0; i < input.length; i++) { for (int j = 0; j < input.length; j++) { if (input[i].equals(input[j]) && i != j) { return true; } } } return false; }我已經設法運行一個代碼,告訴我如果字符串數組中有重復的一系列單詞,它是真還是假,但是,如果沒有一個循環構造也能找到大寫和小寫的重復,我怎么能做到這一點?
2 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
使用Map<String, Integer>
將字符串列表/數組中的單詞作為鍵(小寫單詞)應該可以解決您的問題。如果地圖中已經存在新單詞(使用 來檢查新單詞toLowercase()
),那么您可以返回 true。
您必須顯式或隱式(Java8 隱藏循環或其他集合的構造函數)至少遍歷字符串列表或數組一次(如果不是for-loop
則for-each
循環)以查找是否重復任何單詞。

莫回無
TA貢獻1865條經驗 獲得超7個贊
簡而言之,沒有任何循環就無法做到這一點。但是,如果您正在尋找隱藏循環,則可以使用:
public static boolean out(String[] input) {
return input.length != new HashSet<String>(Arrays.asList(input)).size();
}
如果您需要忽略大小寫,則:
public static boolean out(String[] input) {
return input.length != Arrays.stream(input).map(String::toLowerCase).collect(Collectors.toSet()).size();
}
添加回答
舉報
0/150
提交
取消