[英]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.