小编Tay*_*rie的帖子

为什么我不能为此代码使用Collections.max()函数?-Java

我有十棵树。每棵树都有一些实例变量(空间,网格,适用性和ID);在地理空间和网格上的位置,分别表示从0到1的栖息地适用性的值,以及一个ID号。

我将这些树及其关联的数据放到一个称为树的ArrayList中。然后,我让系统打印出它们的ID和相关的适用性值。代码如下。

最后,我想打印出最高的适用性值,但在max提示下始终出现错误:

“边界不匹配:max(Collection<? extends T>)类型的通用方法Collections不适用于参数(ArrayList<Trees>)。推断的类型Trees不是边界参数的有效替代品<T extends Object & Comparable<? super T>>

我似乎无法弄清楚这意味着什么。如果您需要查看更多代码,请告诉我。

有谁知道我如何获得最高适用性值(最接近1的值)?

int treeCount = 10;
for (int i = 0; i < treeCount; i++) {
    double suitability = RandomHelper.nextDoubleFromTo(0, 1);
    int id = i;
    context.add(new Trees(space, grid, suitability, id));

    ArrayList<Trees> trees = new ArrayList<Trees>();

    Trees tree;

    tree = new Trees(space, grid, suitability, id);
    trees.add(tree);
    System.out.println("Tree " + id + " has a suitability of " + suitability);

    Object obj …
Run Code Online (Sandbox Code Playgroud)

java sorting generics collections comparator

3
推荐指数
1
解决办法
1385
查看次数

java中的最佳练习布尔值

快问,

这是...

this.setPregnant(isPregnant = true);
Run Code Online (Sandbox Code Playgroud)

......和你一样吗?

this.setPregnant(true);
Run Code Online (Sandbox Code Playgroud)

哪一个更好的做法?

@ScheduledMethod(start = 3) 
public void mate() {
    if (this.isFemale == true) {
        Context context = ContextUtils.getContext(this);
        Geography<Agent> geography = (Geography)context.getProjection("Geography");
        Geometry geom = geography.getGeometry(this);
        // get coordinates of the female
        Coordinate femCoord = geom.getCoordinates()[0];
        List<Agent> males = new ArrayList<Agent>();
        //create an envelope around the female
        Envelope envelope = new Envelope (femCoord.x + 0.9, femCoord.x - 0.9, femCoord.y + 0.9, femCoord.y - 0.9);
        //get all the males around the female
        for(Agent male: geography.getObjectsWithin(envelope, …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
96
查看次数

标签 统计

java ×2

collections ×1

comparator ×1

generics ×1

sorting ×1