一个语句中的大于和小于

发布于 2024-10-11 20:30:07 字数 431 浏览 5 评论 0原文

我想知道,你有一个巧妙的方法吗?

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凹づ凸ル 2024-10-18 20:30:07

简单实用方法:

public static boolean isBetween(int value, int min, int max)
{
  return((value > min) && (value < max));
}

Simple utility method:

public static boolean isBetween(int value, int min, int max)
{
  return((value > min) && (value < max));
}
夜空下最亮的亮点 2024-10-18 20:30:07

一些第三方库具有封装范围概念的类,例如 Apache commons-lang 的 Range (和子类)。

使用这样的类,您可以表达类似于以下内容的约束:

if (new IntRange(0, 5).contains(orderBean.getFiles().size())
// (though actually Apache's Range is INclusive, so it'd be new Range(1, 4) - meh

还有一个额外的好处,即范围对象可以在类中的其他位置定义为常量值。

然而,如果不引入其他库并使用它们的类,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:

if (new IntRange(0, 5).contains(orderBean.getFiles().size())
// (though actually Apache's Range is INclusive, so it'd be new Range(1, 4) - meh

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.

贩梦商人 2024-10-18 20:30:07

如果 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).

梦幻的心爱 2024-10-18 20:30:07

这是一种丑陋的方法。我只想使用局部变量。

编辑:如果 size() > 0也是如此。

if (orderBean.getFiles().size() + Integer.MIN_VALUE-1 < Integer.MIN_VALUE + 5-1)

This is one ugly way to do this. I would just use a local variable.

EDIT: If size() > 0 as well.

if (orderBean.getFiles().size() + Integer.MIN_VALUE-1 < Integer.MIN_VALUE + 5-1)
笑梦风尘 2024-10-18 20:30:07

java不是python。

你不能做这样的事情

if(0 < i < 5) or if(i in range(0,6))

的方法:

int i = getFilesSize();
if(0 < i && i < 5){
    //operations

}

你提到的最简单

   if(0 < i){
       if(i < 5){
          //operations
       }
     }

java is not python.

you can't do anything like this

if(0 < i < 5) or if(i in range(0,6))

you mentioned the easiest way :

int i = getFilesSize();
if(0 < i && i < 5){
    //operations

}

of

   if(0 < i){
       if(i < 5){
          //operations
       }
     }
允世 2024-10-18 20:30:07

如果这确实困扰您,为什么不编写自己的方法 isBetween(orderBean.getFiles().size(),0,5)

另一种选择是使用 isEmpty,因为它更清晰:

if(!orderBean.getFiles().isEmpty() && orderBean.getFiles().size() < 5)

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:

if(!orderBean.getFiles().isEmpty() && orderBean.getFiles().size() < 5)
七堇年 2024-10-18 20:30:07

请在某处编写一个静态方法并编写:

if( isSizeBetween(orderBean.getFiles(), 0, 5) ){
    // do your stuff 
}

Please just write a static method somewhere and write:

if( isSizeBetween(orderBean.getFiles(), 0, 5) ){
    // do your stuff 
}
宫墨修音 2024-10-18 20:30:07

现在你可以使用 lodash

var x = 1;
var y = 5;
var z = 3;
_.inRange(z,x,y);
//results true

Nowadays you can use lodash:

var x = 1;
var y = 5;
var z = 3;
_.inRange(z,x,y);
//results true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文