简体   繁体   English

Java中数组中的最大元素(整数数组的集合.max()int [])

[英]maximal element in an array in Java (Collections.max() for integer arrays int[])

有没有像Collections.max那样在标准java运行时库中找到常规数组的数组中的最大值?

If you have an array of Objects you can use 如果您有一个可以使用的对象数组

Collections.max(Arrays.asList(array));

If you have an array of primitive you can just use a simple loop. 如果你有一个原始数组,你可以使用一个简单的循环。

long[] array;
long max = array[0];
for(long l : array) if (max < l) max = l;

You could use Arrays.sort(int[]) and then access the first (or last) element of it. 您可以使用Arrays.sort(int [])然后访问它的第一个(或最后一个)元素。 Or you could simply iterate over the array and look for the largest/biggest element. 或者你可以简单地迭代数组并寻找最大/最大的元素。 That's basically a no-brainer. 这基本上是一个明智的选择。

No, there is no Arrays.max or a lookalike, at least in Java 6. 不,至少在Java 6中没有Arrays.max或类似的东西。

If you look at the signature and implementation of Collections.max, it makes quite heavy use of parameterized types. 如果你看一下Collections.max的签名和实现,它会大量使用参数化类型。 In Java, generic arrays are problematic to say the least, so maybe therefore it is not a good idea to provide a generic max implementation for arrays in Java, and keep the focus on (generic) collections. 在Java中,通用数组至少可以说是有问题的,因此可能因此在Java中为数组提供通用的最大实现并且将重点放在(通用)集合上并不是一个好主意。

Edit: as newacct correctly points out, the usage of generic arrays is not necessarily more problematic than the usage of generic collections, so I've edited the above text since the original was wrong. 编辑:正如newacct正确指出的那样,泛型数组的使用并不一定比使用泛型集合更有问题,所以我编辑了上面的文本,因为原来是错误的。 Still, the main argument of "generic arrays are problematic" is still valid in my opinion, and collections should be preferred over reference type arrays. 尽管如此,在我看来,“通用数组有问题”的主要论点仍然有效,并且集合应优先于引用类型数组。

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
    if (comp==null)
        return (T)max((Collection<SelfComparable>) (Collection) coll);

Iterator<? extends T> i = coll.iterator();
T candidate = i.next();

    while (i.hasNext()) {
    T next = i.next();
    if (comp.compare(next, candidate) > 0)
    candidate = next;
}
return candidate;
}

You could also create a decorator for Collection which contains extra methods like getMaximumValue() and make it update the value returned every time element is added/removed if there's need. 您还可以为Collection创建一个装饰器 ,它包含额外的方法,如getMaximumValue(),并且如果需要,每次添加/删除元素时都会更新返回的值。

This would only make sense though if you'd use the maximum value a lot in your program which would mean that iterating through the list every time would cause significant overhead. 这只会有意义,但是如果你在程序中大量使用最大值,这意味着每次迭代列表都会导致很大的开销。

As far as I know, no. 据我所知,没有。 You could look at asList ( http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...) ), but it's probably not worth it. 您可以查看asList( http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T ...) ),但它可能不值得。

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

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