一个语句中的大于和小于
我想知道,你有一个巧妙的方法吗?
if(orderBean.getFiles().size() > 0 && orderBean.getFiles().size() < 5)
不声明其他地方不需要的变量?
int filesCount = orderBean.getFiles().size();
if(filesCount > 0 && filesCount < 5) {
我的意思是,在 for 循环中,我们为实际迭代“声明条件”,可以声明一个变量,然后指定条件。在这里,一个人做不到,也做不到类似的事情
if(5 > orderBean.getFiles().size() > 0)
I was wondering, do you have a neat way of doing this ?
if(orderBean.getFiles().size() > 0 && orderBean.getFiles().size() < 5)
without declaring a variable that is not needed anywhere else ?
int filesCount = orderBean.getFiles().size();
if(filesCount > 0 && filesCount < 5) {
I mean, in for loop we are "declaring conditions" for the actual iteration, one can declare a variable and then specify the conditions. Here one can't do it, and neither can do something like
if(5 > orderBean.getFiles().size() > 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
简单实用方法:
Simple utility method:
一些第三方库具有封装范围概念的类,例如 Apache commons-lang 的 Range (和子类)。
使用这样的类,您可以表达类似于以下内容的约束:
还有一个额外的好处,即范围对象可以在类中的其他位置定义为常量值。
然而,如果不引入其他库并使用它们的类,Java 强大的语法意味着您无法通过调整语言本身来很好地提供此功能。而且(在我看来),仅仅为了这么少量的语法糖而引入第三方库是不值得的。
Several third-party libraries have classes encapsulating the concept of a range, such as Apache commons-lang's Range (and subclasses).
Using classes such as this you could express your constraint similar to:
with the added bonus that the range object could be defined as a constant value elsewhere in the class.
However, without pulling in other libraries and using their classes, Java's strong syntax means you can't massage the language itself to provide this feature nicely. And (in my own opinion), pulling in a third party library just for this small amount of syntactic sugar isn't worth it.
如果 getFiles() 返回一个 java.util.Collection,则 getFiles().isEmpty() &&尺寸<5就可以了。
另一方面,除非你封装了提供诸如boolean sizeBetween(int min, int max)之类的方法的容器。
If getFiles() returns a
java.util.Collection
, !getFiles().isEmpty() && size<5 can be OK.On the other hand, unless you encapsulate the container which provides method such as
boolean sizeBetween(int min, int max)
.这是一种丑陋的方法。我只想使用局部变量。
编辑:如果 size() > 0也是如此。
This is one ugly way to do this. I would just use a local variable.
EDIT: If size() > 0 as well.
java不是python。
你不能做这样的事情
的方法:
你提到的最简单
java is not python.
you can't do anything like this
you mentioned the easiest way :
of
如果这确实困扰您,为什么不编写自己的方法
isBetween(orderBean.getFiles().size(),0,5)
?另一种选择是使用 isEmpty,因为它更清晰:
If this is really bothering you, why not write your own method
isBetween(orderBean.getFiles().size(),0,5)
?Another option is to use
isEmpty
as it is a tad clearer:请在某处编写一个静态方法并编写:
Please just write a static method somewhere and write:
现在你可以使用 lodash:
Nowadays you can use lodash: