Xtend循环中断

5
在Xtend中,是否可以在循环中使用break或者检查以停止循环?
«FOR e:d.entitys»
    «FOR a:e.attributes»
        «IF a.eClass.name.contentEquals('Something')»
            «e.name» "This output should be output one for each Entity e"
        «ENDIF»
    «ENDFOR»
«ENDFOR»

我的输出是:

Entity 1 "This output should be output one for each Entity e"
Entity 1 "This output should be output one for each Entity e"
Entity 1 "This output should be output one for each Entity e"
Entity 2 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"

但是我想要的是:

Entity 1 "This output should be output one for each Entity e"
Entity 2 "This output should be output one for each Entity e"
Entity 4 "This output should be output one for each Entity e"

如何实现我想要的输出?我听说可以调用另一个方法或类似的东西,但我不知道如何做到这一点,有人能为我展示一些解决此问题的代码吗? 谢谢 :)

1个回答

0
你可以使用一个集合来存储已经访问过的条目。例如,考虑以下程序:
def static void main(String... args) {
    val list = #['my', 'possibly', 'possibly', 'duplicated', 'duplicated', 'duplicated', 'entities']
    val visited = new LinkedHashSet
    println(
    '''«FOR a:list»
        «IF visited.add(a)»
            «a» "This output should be output one for each Entity e"
        «ENDIF»
    «ENDFOR»''')
}

它输出:

my "This output should be output one for each Entity e"
possibly "This output should be output one for each Entity e"
duplicated "This output should be output one for each Entity e"
entities "This output should be output one for each Entity e"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接