[英]Equivalent of Python's 'if … in …' in Java?
Python 有一個 'if...in...' 語句,它允許一個人創建一個列表,並且使用 'if...in...' 語句這個人可以檢查列表中是否存在指定的內容. 例如,檢查元音列表中是否存在特定字母。 所以基本上:
vowels = [list-of-vowels]
if 'e' in vowels:
(do something)
那么在 Java 中是否有任何簡單的等效於 Python 的“if in”語句?
容器中有一個contains
方法:
if ( container.contains( element ) ){
....
}
對於列表,您可以使用 .contains() 方法
對於數字,您需要手動建立一個范圍(即if(i>0 && i <10)
)
我不確定你是不是假裝...
char vowels[] = {'a', 'b', 'c', 'd', 'e'};
for (int i = 0; i < vowels.length; i++) {
if (vowels[i] == 'e') {
System.out.println("We find the e!!!!!");
}
}
我認為這更接近 python 的“in”,它完全符合你的要求,你可以為每個類型的數據進行更改,你想要......
char vowels[] = {'a', 'b', 'c', 'd', 'e'};
for (char Value : vowels) {
if (Value == 'e') {
System.out.println("We find the e!!!!!");
}
}
您可以使用 ffg 代碼。
String v = "aeiou";
if(v.containsIgnoreCase("e"){
System.out.println("e located sir/mam");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.