我有一个这样的嵌套循环结构:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break; // Breaks out of the inner loop
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我怎样才能摆脱两个循环?我看过类似的问题,但没有一个特别关注Java.我无法应用这些解决方案,因为大多数使用了gotos.
我不想把内循环放在不同的方法中.
我不想重新运行循环.当我完成循环块的执行时.
我想知道当验证结束条件(不同于正确的迭代次数)时是否可以在C++中结束for循环.例如:
for (int i = 0; i < maxi; ++i)
for (int j = 0; j < maxj; ++j)
// But if i == 4 < maxi AND j == 3 < maxj,
// then jump out of the two nested loops.
Run Code Online (Sandbox Code Playgroud)
我知道这可能在Perl中使用下一个LABEL或最后一个LABEL调用和标记的块,是否可以在C++中执行它或者我应该使用while循环?
谢谢.
嗨,我是java的初学者,我的程序有4个for循环:我的程序就像这样,如果b是true,该元素将从pointList中删除,并且n将是n-- 我想要从所有for循环中出来并从第一个出来对于循环,这样l会l++,我怎么能做到这一点?与休息声明?
for (int l = 0; l < n; l++) {
for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), …Run Code Online (Sandbox Code Playgroud) 我已将以下方法添加到Array原型中:
Array.prototype.foreach = function(func){
for(var i = 0; i < this.length; i++){
if(!func(this[i]) === false) break; //return false from func in order to break the loop
}
return this;
}
Run Code Online (Sandbox Code Playgroud)
在同一个文件中,在上面的代码之后,我有以下jQuery插件:
jQuery.fn.addClassForEvents = function(){
var that = this;
arguments.foreach(function(event){
that.bind(event[0], function(){
that.addClass(event[0]);
})
.bind(event[1], function(){
that.removeClass(event[0]);
});
});
return this;
}
Run Code Online (Sandbox Code Playgroud)
为了使用这个jQuery插件,我的代码看起来像:
$('div').addClassForEvents(['mouseenter', 'mouseleave']);
Run Code Online (Sandbox Code Playgroud)
但是,浏览器在jQuery插件的"arguments.foreach(...."行中抛出一个错误,简单地说明了
对象#没有方法'foreach'
然而,该foreach方法适用于我的代码的其他地方.为什么在这个jQuery插件中未定义?