5

在 Xtend 中,是否可以中断循环或进行检查以中断循环?

«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"

如何实现我想要的输出?我听说您可以调用其他方法或其他方法,但我不知道该怎么做,有人可以给我一些解决这个问题的代码吗?谢谢 :)

4

1 回答 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"
于 2017-04-28T15:47:39.663 回答