Groovy - how to exit each loop?

前端 未结 4 1649
粉色の甜心
粉色の甜心 2021-02-03 21:39

I\'m new to Grails/Groovy and am trying to find a node in a an xml file; I\'ve figured out how to iterate over all of them, but I want to exit the loop when the target node is f

4条回答
  •  灰色年华
    2021-02-03 22:33

    Replace each loop with any or find closure.

    def list = [1, 2, 3, 4, 5]
    list.any { element ->
        if (element == 2)
            return // continue
    
        println element
    
        if (element == 3)
            true // break
    }
    

    Output

    1
    3
    

提交回复
热议问题