所以我很难理解增强的循环..
编写一个名为contains()的方法,它接受一个整数数组和一个整数值.如果数组包含指定的值,则该方法应返回true,否则返回false.
boolean contains (int [] x, int y) {
for (y : x) {
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道他们是如何工作的我显然需要回报一些东西?
如果要检查y数组中是否包含数字x,则应执行以下操作:
boolean contains (int [] x, int y) {
for (int i : x) {
if (i == y) return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
for通常称这种类型的循环for-each.的int i : x基本含义Go through the loop 'x' and name the current variable 'i'.然后,如果当前变量i等于您要查找的变量(在您的情况下 - y),那么return true.
| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |