How to compare values stored in List<Long> with a long value?

老子叫甜甜 提交于 2019-12-13 22:00:17

问题


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

Here is the logged out value of List<Long> l:

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

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

Here's how:

    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 {
            }

    }

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 '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?


回答1:


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.

What you are doing:

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

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

What you should do:

Simply compare your l.get(i) with Long by doing 900L

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

Or

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


来源:https://stackoverflow.com/questions/41023421/how-to-compare-values-stored-in-listlong-with-a-long-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!