如何比较List< Long>中存储的值价值很高? [英] How to compare values stored in List<Long> with a long value?

查看:169
本文介绍了如何比较List< Long>中存储的值价值很高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检索一些长值并将其存储在列表< Long>升

I'm retrieving some long values and storing it in List<Long> l.

以下是 List< Long>的注销值l

D/lng: [2197, -1007, 4003]

然后,我试图将它与 long 之类的值进行比较< = 900 > 900

Then, I'm trying to compare it with long values like <=900 or >900.

方法如下:

    public void filterRequests(final List<Long> l) {

            final int size = l.size();
            int i;

            for (i = 0; i < size; i++){

            }
            Log.d("lng", String.valueOf(l));

            if (l.get(i) <= 900) {
                 // do the logic when l <= 900       
            } else {
            }

    }

问题是 if(l如果(l.get(i)>> 900)即使在 l.get(i)的值均为< = 900 > 900 ,见: D / lng:[2197,-1007,4003]

The problem is that if (l.get(i) <= 900) or if (l.get(i) > 900) both is not working even when the l.get(i) has values both <=900 and >900, see: D/lng: [2197, -1007, 4003]

UPDATE :@HarisQureshi的答案有效,但问题是的原因是循环,如果内部条件被调用3次,而我希望它只被调用一次。所以,我想知道有没有办法在该代码上面定义 for 循环,然后使用 i in if(l.get(i)< = 900) if(l.get(i)> 900)条件?

UPDATE: @HarisQureshi 's answer worked but the problem is that cause of the for loop, the if condition inside is getting called 3 times while I want it to get called only once. So, I want to know that is there a way to define for loop above that code and then use the i in the if (l.get(i) <= 900) and if (l.get(i) > 900) conditions?

推荐答案

你用 Long 制作了一个列表, List Long 都不是原始数据类型。
在您的问题中,您正在比较原始数据类型和非原始数据类型,为了进行比较,比较器的两侧应该具有相同的数据类型。

You made a list with Long, both List and Long are not primitive data types. In your question you are comparing primitive and non primitive data types, for comparison both sides of a comparator should be of same data types.

什么你在做什么:

if (l.get(i) <= 900)

此处 l.get(i)返回值,其中 900 是整数值。

Here l.get(i) returns a Long value where as 900 is integer value.

什么你应该这样做:

只需将 l.get(i)与<$ c $进行比较c>长做 900L

if (l.get(i) <= 900L)

if (l.get(i) > 900L)

这篇关于如何比较List&lt; Long&gt;中存储的值价值很高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆