Groovy没有收藏(Groovy not in collection)

编程入门 行业动态 更新时间:2024-10-28 20:21:34
Groovy没有收藏(Groovy not in collection)

查看列表中是否有东西的常规方式是使用“in”

if('b' in ['a','b','c'])

然而,你如何很好地看到某件物品是否不在收藏品中?

if(!('g' in ['a','b','c']))

似乎凌乱和“!” 隐藏在随便的一瞥中。 有没有更习惯性的常规方式来做到这一点?

谢谢!

The groovy way to see if something is in a list is to use "in"

if('b' in ['a','b','c'])

However how do you nicely see if something is not in a collection?

if(!('g' in ['a','b','c']))

Seems messy and the "!" is hidden to the casual glance. Is there a more idiomatic groovy way to do this?

Thanks!

最满意答案

不幸的是,我认为没有很好的语法。 但是你可以使用一个辅助变量来使其更具可读性:

def isMagicChar = ch in ['a', 'b', 'c'] if (!isMagicChar) ...

或者,在这种情况下,你可以使用正则表达式:)

if (ch !=~ /[abc]/) ...

I think there is no not in pretty syntax, unfortunately. But you can use a helper variable to make it more readable:

def isMagicChar = ch in ['a', 'b', 'c'] if (!isMagicChar) ...

Or, in this case, you may use a regex :)

if (ch !=~ /[abc]/) ...

更多推荐

本文发布于:2023-07-16 09:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收藏   Groovy   collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!