Java/Groovy double for 语句问题
这是一个 Java/Groovy 问题,我正在执行此功能以在我的网站的搜索机制中实现。我有两个列表:
String [] lista = temp.split() // ignore the temp part
String [] searchLista = search.split() // ignore the search part
基本上,列表是这样的:
lista = {a, b, c, d}
searchLista= {a, b, a, d}
boolean test
我想验证列表“lista”上的任何元素是否与“searchLista”上的相同。为此,我执行了以下功能:
for(int i = 0; i< lista.length-1; i++){
for(int j = 0; j< searchLista.length-1; j++) {
if(lista[i].contains(searchLista[j])){
test = true
##
}
}
}
我的问题是,如果此验证为真:'lista[i].contains(searchLista[j])',布尔变量测试变为真,接下来我想跳到两个 for 之外。在##处进行简单的“中断”就可以了吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(顺便问一下,您是否故意遗漏了两个列表的最后一个元素?)
正常的
break
语句只会退出内部循环。如果要中断两个级别,有以下三个常见选项:
的中断 示例最后一个选项:
就我个人而言,我不经常使用标签 - 如果可能的话,我宁愿使用“使整个双循环成为一个方法”选项。
(Are you deliberately missing out the last element of both lists, by the way?)
A normal
break
statement would just exit the inner loop.If you want to break two levels, there are three common options:
Example of the last option:
Personally I don't use labels very often - I would rather use the "make the whole double loop a method" option if possible.
如果您只是想找出这两个数组是否有任何共同元素,最常用的方法是:
以下是对上面的函数/方法的一些测试
您可以在Groovy Console 验证其是否有效
If you simply want to find out whether the 2 arrays have any elements in common, the Groovyiest way to do this is:
Here are some tests for the function/method above
You can run the code above in the Groovy Console to verify it works
简短的答案是“否” - 尽管稍长的答案是“这只会影响性能,但不会影响正确性,因为一旦您(最终)退出循环,
test
仍将是true
”。原因是
break
只能跳出直接封闭的循环。在这种情况下,您有双重嵌套循环并且想要打破这两个循环。实现此目的的一种方法是使用标签:其他替代方法是将外部循环的条件更改为
i
i
i
i
i
i
i
i
i
i
i
i l;ista.length - 1 && !test
,或者将这一切包装到一个函数中,您可以简单地返回
。然而,由于它是 Groovy,我认为会有一种 Groovier 方式(懒惰地)计算两个集合的交集 - 此时您可以测试交集是否为空(这就是您真正的意思)在此之后)。写起来肯定更清晰
,而且在性能方面也可能不会更差。
The short answer is No - though the slightly longer answer is "this only affects performance, though, not correctness since
test
will still betrue
once you (eventually) exit the loops".The reason is that
break
only breaks out of the directly enclosing loop. In this case, you have doubly-nested loops and want to break out of both of them. One way to do this, is with a label:Other alternatives would be to change to condition on the outer loop to
i < l;ista.length - 1 && !test
, or to wrap this all up into a function from which you can simplyreturn
.Since it's Groovy however, I'd have thought there'd be a Groovier way to (lazily) calculate the intersection of two sets - at which point you can just test whether the intersection is empty or not (which is what you're really after here). It's definitely clearer to write
and this may be no worse in terms of performance too.
一个简单的中断只会中断内部的 for 循环。
但您可以在自己的函数中执行此操作并在##处添加返回语句。
a simple break would only break the inner for loop.
but you could do this in a own function and add a return statement at ##.
您可以使用 org.apache.commons.collections.CollectionUtils.intersection(Collection, Collection) 来代替。首先将每个数组放入
Set
中。You could use
org.apache.commons.collections.CollectionUtils.intersection(Collection, Collection)
instead. Put each array into aSet<String>
first.使用 Groovy 执行此操作的最简单方法是:
The simplest way to do this with Groovy would be: