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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/393017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:12:10  来源:igfitidea点击:

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

javastlcollections

提问by Peter Lawrey

Is there anything like Collections.maxwhich finds the maximal value in an array for regular arrays in the standard java runtime library?

是否有类似的东西Collections.max可以在标准 Java 运行时库中找到常规数组的数组中的最大值?

采纳答案by eljenso

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. 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.

如果您查看 Collections.max 的签名和实现,它会大量使用参数化类型。在 Java 中,泛型数组至少可以说是有问题的,因此也许因此在 Java 中为数组提供泛型 max 实现并专注于(泛型)集合并不是一个好主意。

Edit: as newacct correctly points out, the usageof 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. Still, the main argument of "generic arrays are problematic" is still valid in my opinion, and collections should be preferred over reference type arrays.

编辑:正如 newacct 正确指出的那样,泛型数组的使用不一定比泛型集合的使用更成问题,所以我编辑了上面的文本,因为原文是错误的。尽管如此,“通用数组有问题”的主要论点在我看来仍然有效,并且集合应该比引用类型数组更受欢迎。

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

回答by Peter Lawrey

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;

回答by Bombe

You could use Arrays.sort(int[])and then access the first (or last) element of it. Or you could simply iterate over the array and look for the largest/biggest element. That's basically a no-brainer.

您可以使用Arrays.sort(int[])然后访问它的第一个(或最后一个)元素。或者您可以简单地遍历数组并查找最大/最大的元素。这基本上是不费吹灰之力的。

回答by Esko

You could also create a decoratorfor 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.

这只有在您在程序中大量使用最大值时才有意义,这意味着每次遍历列表都会导致显着的开销。

回答by Matthew Flaschen

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...)),但这可能不值得。