【发布时间】:2017-04-29 23:18:23
【问题描述】:
Collections.max(arraylist) 不起作用,常规的 for 循环也不起作用。
我拥有的是:
ArrayList<Forecast> forecasts = current.getForecasts();
Collections.max(forecast) 给我这个错误:
The method max(Collection<? extends T>) in the type Collections is
not applicable for the arguments (ArrayList<Forecast>)
ArrayList 包含 Forecast 对象,每个对象都有一个 int 字段来表示每天的温度。我正在尝试将最大值存储在 int max 中。
【问题讨论】:
-
Forecast 类是否实现 Comparable?
-
Forecast 的
forecastsArrayList 不是 int 数组。如果使用得当,循环和max()都能正常工作。 -
你必须使用 Compactor 接口
-
不使用常规 for 循环如何工作?你能分享你试过的代码吗?
-
@PeterLawrey 你能详细说明一下吗?如果预测不是整数数组,我怎样才能得到其中的最大整数值?