如何比较存储在 List<Long> 中的值具有长期价值? [英] How to compare values stored in List<Long> with a long value?
问题描述
我正在检索一些长值并将其存储在 List
.
I'm retrieving some long values and storing it in List<Long> l
.
这是List
:
D/lng: [2197, -1007, 4003]
然后,我尝试将它与 <=900
或 >900
等 long
值进行比较.
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.get(i) <= 900)
或 if (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 的回答有效,但问题是 for
循环的原因,里面的 if
条件被调用3 次,而我希望它只被调用一次.所以,我想知道有没有办法在该代码上方定义 for
循环,然后在 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,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)
返回一个 Long
值,其中 900
是整数值.
Here l.get(i)
returns a Long
value where as 900
is integer value.
你应该做什么:
通过执行 900L
if (l.get(i) <= 900L)
或者
if (l.get(i) > 900L)
这篇关于如何比较存储在 List<Long> 中的值具有长期价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!