使用集合的数组的Java-Min和max值

时间:2014-11-08 13:26:12

标签: java

我试图从一维数组中获取最大值和最小值

double[] source = {1.2,1.3,1.9,0.9,0.10};
double[][] maxar = new double[10][ 5];
double x=Collections.max(source);
double y=Collections.min(source);

编译器运行时错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method max(Collection<? extends T>) in the type Collections is not applicable for the arguments (double[])


The method min(Collection<? extends T>) in the type Collections is not applicable for the arguments (double[])

任何帮助都将受到高度赞赏?

1 个答案:

答案 0 :(得分:0)

你可以使用apache commons lang包并解决同样的问题。

        Double[] doubleArray = ArrayUtils.toObject(source);
        List<Double> list = Arrays.asList(doubleArray);

        double x=Collections.max(list);
        double y=Collections.min(list);

        System.out.println(x);
        System.out.println(y);