Java/Groovy double for 语句问题

发布于 2024-11-09 16:08:24 字数 800 浏览 0 评论 0 原文

这是一个 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 之外。在##处进行简单的“中断”就可以了吗?

This is a Java/Groovy question, im doing this function to implement in a search mechanism in my website. I have two lists:

String [] lista = temp.split() // ignore the temp part
String [] searchLista = search.split() // ignore the search part

Basically, the lists are something like this:

    lista = {a, b, c, d}
    searchLista= {a, b, a, d}
    boolean test

I want to verify if, any element on list 'lista' is the same on 'searchLista'. For that i did the following function:

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
                        ##
                    }
                }
            }

My question is, if this validation is true: 'lista[i].contains(searchLista[j])', boolean variable test becomes true and next i want to jump outside both fors. A simple 'break' in the place of the ##s will do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

于我来说 2024-11-16 16:08:24

(顺便问一下,您是否故意遗漏了两个列表的最后一个元素?)

正常的 break 语句只会退出内部循环。

如果要中断两个级别,有以下三个常见选项:

  • 将外部循环的条件设为可以在主体中设为 false 的条件
  • 将整个循环设为一个方法,然后从中返回
  • 使用带标签

的中断 示例最后一个选项:

// Code before loop
outerLoop:
for (...) {
    for (...) {
        if (...) {
            break outerLoop;
        }
    }
}

就我个人而言,我不经常使用标签 - 如果可能的话,我宁愿使用“使整个双循环成为一个方法”选项。

(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:

  • Make the condition for the outer loop something which you can make false in the body
  • Make the whole loop a method, and just return from it
  • Use a break with a label

Example of the last option:

// Code before loop
outerLoop:
for (...) {
    for (...) {
        if (...) {
            break outerLoop;
        }
    }
}

Personally I don't use labels very often - I would rather use the "make the whole double loop a method" option if possible.

羅雙樹 2024-11-16 16:08:24

如果您只是想找出这两个数组是否有任何共同元素,最常用的方法是:

boolean haveElementsInCommon(array, otherArray) {
  !array.toList().disjoint(otherArray.toList())
}

以下是对上面的函数/方法的一些测试

// create some test data
String[] list1 = [1,2,3,4]
String[] list2 = [5,6,7,8]
String[] list3 = [8,9,10,11]

// test the function works for arrays with nothing in common           
assert !haveElementsInCommon(list1, list2)
// test the function works for arrays with at least one element in common    
assert haveElementsInCommon(list2, list3)

您可以在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:

boolean haveElementsInCommon(array, otherArray) {
  !array.toList().disjoint(otherArray.toList())
}

Here are some tests for the function/method above

// create some test data
String[] list1 = [1,2,3,4]
String[] list2 = [5,6,7,8]
String[] list3 = [8,9,10,11]

// test the function works for arrays with nothing in common           
assert !haveElementsInCommon(list1, list2)
// test the function works for arrays with at least one element in common    
assert haveElementsInCommon(list2, list3)

You can run the code above in the Groovy Console to verify it works

酷炫老祖宗 2024-11-16 16:08:24

简短的答案是“否” - 尽管稍长的答案是“这只会影响性能,但不会影响正确性,因为一旦您(最终)退出循环,test仍将是true ”。

原因是 break 只能跳出直接封闭的循环。在这种情况下,您有双重嵌套循环并且想要打破这两个循环。实现此目的的一种方法是使用标签:

OUTER: 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
          break OUTER;
      }
   }
}

其他替代方法是将外部循环的条件更改为 i i i i i i i i i i i i l;ista.length - 1 && !test,或者将这一切包装到一个函数中,您可以简单地返回

然而,由于它是 Groovy,我认为会有一种 Groovier 方式(懒惰地)计算两个集合的交集 - 此时您可以测试交集是否为空(这就是您真正的意思)在此之后)。写起来肯定更清晰

test = !intersection(lista, searchLista).isEmpty()

,而且在性能方面也可能不会更差。

The short answer is No - though the slightly longer answer is "this only affects performance, though, not correctness since test will still be true 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:

OUTER: 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
          break OUTER;
      }
   }
}

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 simply return.

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

test = !intersection(lista, searchLista).isEmpty()

and this may be no worse in terms of performance too.

雪化雨蝶 2024-11-16 16:08:24

一个简单的中断只会中断内部的 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 ##.

白况 2024-11-16 16:08:24

您可以使用 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 a Set<String> first.

悲凉≈ 2024-11-16 16:08:24

使用 Groovy 执行此操作的最简单方法是:

lista.any { it in searchLista }

The simplest way to do this with Groovy would be:

lista.any { it in searchLista }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文