Java 相当于Python';s';如果。。。。。在;在爪哇?
Python有一个“如果…”。。。在…语句中,允许用户创建列表,并使用“如果…”。。。在…'语句中,该人员可以检查列表中是否存在指定的内容。例如,检查元音列表中是否存在特定字母。所以基本上:Java 相当于Python';s';如果。。。。。在;在爪哇?,java,arrays,list,if-statement,python-3.x,Java,Arrays,List,If Statement,Python 3.x,Python有一个“如果…”。。。在…语句中,允许用户创建列表,并使用“如果…”。。。在…'语句中,该人员可以检查列表中是否存在指定的内容。例如,检查元音列表中是否存在特定字母。所以基本上: vowels = [list-of-vowels] if 'e' in vowels: (do something) 那么,在Java中是否有类似Python的“if in”语句的简单的等价物呢?容器中有一个contains方法: if ( container.contains( element
vowels = [list-of-vowels]
if 'e' in vowels:
(do something)
那么,在Java中是否有类似Python的“if in”语句的简单的等价物呢?容器中有一个
contains
方法:
if ( container.contains( element ) ){
....
}
对于列表,可以使用.contains()方法
对于数字,您需要手动建立一个范围(即,
if(i>0&&i我不确定您是否假装
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!!!!!");
}
}
char元音[]={'a','b','c','d','e'};
for(int i=0;i<元音.length;i++){
if(元音[i]=“e”){
System.out.println(“我们找到了e!!!”;
}
}
我认为这更接近python的“in”,它完全满足您的需要,您可以为每个人更改您想要的数据类型
char vowels[] = {'a', 'b', 'c', 'd', 'e'};
for (char Value : vowels) {
if (Value == 'e') {
System.out.println("We find the e!!!!!");
}
}
您可以使用f.f.g代码
String v = "aeiou";
if(v.containsIgnoreCase("e"){
System.out.println("e located sir/mam");
}
那么“contains”呢?也许看看你正在尝试使用的类api吧..nit:在…
语句中没有if.。有if
语句和in
操作符+1,因为对我们Java人来说显而易见的东西对Python人来说可能并不明显。当我第一次开始使用Python时(从Java背景开始),我有时会感到困惑,为什么py集合上没有contains
方法;我忘了python为此使用了一种语言构造。因此,我同情那些反过来找不到语言构造的人,忘记了在Java中它是通过普通的ol'方法完成的。