简体   繁体   English

将int与整数的arraylist比较

[英]Comparing int to arraylist of integer

I have an int element and I would like to know if this int is higher than all the integers of an Arraylist ? 我有一个int元素,我想知道这个int是否高于Arraylist的所有整数?

Example : 范例:

int a ; // Contain an int (i.e 51 or 54 or 989...etc)

ArrayList<Integer> list = new ArrayList<Integer>(); // contain a list of Integers

My purpose is to know if a is higher than any number in the arraylist. 我的目的是知道a是否大于arraylist中的任何数字。

Thanks in advance 提前致谢

Sorting is complete overkill. 排序是完全过分的。 You just need to compare your value to the highest value in the list. 您只需要将您的价值与列表中的最高价值进行比较即可。 Here's a way to do this with existing library functions: 这是使用现有库函数执行此操作的方法:

if (a > Collections.max(list)) {
   //a is higher than anything in list
}

Note: 注意:

This carries a caveat of always going through the whole list even if the very first element is larger than a . 即使第一个元素大于a也要始终遍历整个列表。 But it's a tradeoff you're usually willing to make because the code reads so nice. 但这是您通常愿意进​​行的折衷,因为代码读起来非常好。 If you really want the early exit, you can roll your own approach like in Austin's answer, or in Java 8, it would look something like this: 如果您确实希望早日退出,则可以采用自己的方法,例如在Austin的答案中或在Java 8中,它看起来像这样:

if ( list.stream().allMatch(element -> a > element) ) {
  //...a is higher than anything in list
}

you can just iterate the array to see if any other value is higher.. 您可以仅迭代数组以查看其他值是否更高。

int a = _whateverInt ; // Contain an int (i.e 51 or 54 or 989...etc)

ArrayList<Integer> list = new ArrayList<Integer>();
boolean isHigher = true;
for(int i = 0; i < list.size() && isHigher; i ++)
{
    isHigher = a > list.get(i);

}

Solution 1: 解决方案1:

public class Test {
    public static void main(String[] args) {
        int a = 150;

        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(50);
        arrayList.add(100);
        arrayList.add(30);

        System.out.println(a > Collections.max(arrayList));
    }
}

Solution 2: 解决方案2:

public class Test {
    public static void main(String[] args) {
        int a = 150;

        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(50);
        arrayList.add(100);
        arrayList.add(30);

        Collections.sort(arrayList);
        System.out.println(a > arrayList.get(arrayList.size() - 1));
    }
}
int a;

ArrayList<Integer> list = new ArrayList<Integer>();

int max = Collections.max(list);

if(a>max)
{
//this is what you want
}

Hope you find this useful... edit: oops someone already answered the same trick :( 希望您发现这个有用...编辑:哎呀,某人已经回答了相同的技巧

private boolean isLargest(int a, ArrayList<Integer> list)
{
   ArrayList<Integer> sortedList = Collections.sort(list);
   if(a > sortedList.get(0))
      return true;
   return false;
}

It is not efficient, but this approach leaves the ordering in the original list in-tact. 它效率不高,但是这种方法可以完整保留原始列表中的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM