Collections.max() 不正确的参数

发布于 2024-10-24 09:55:55 字数 383 浏览 0 评论 0原文

我只是想找到“官员”列表的最大值。我正在处理的任务的规格不允许我对军官进行比较,因此我使用比较器来比较它们。

然而,编译器抱怨我的参数类型。谁能看出出了什么问题吗? (不用担心返回...我还没有这样做。)

在下面的片段之外,军官是列表<军官>;已初始化。

Collections.max(officers, new Comparator<Officer>()
    {
        public int compare( Officer a, Officer b )
        {
           return -1; //will do after
        }
    }
);

任何建议将不胜感激!

I'm simply trying to find the max of a list of "Officers". The specs of the assignment I'm working on don't allow me to make the Officers comparable, so instead I'm using a Comparator to compare them.

However, the compiler is complaining about the types of my arguments. Can anyone see what's wrong? (Don't worry about the return... I haven't done that yet.)

Outside of the fragment below, officers is a List<Officer> which has been initialized.

Collections.max(officers, new Comparator<Officer>()
    {
        public int compare( Officer a, Officer b )
        {
           return -1; //will do after
        }
    }
);

Any suggestions would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-10-31 09:55:55

这编译得很好。这是 ideone.com 演示

import java.util.*;

class Officer {
}

public class Test {

    public static void main(String[] args) {

        List<Officer> officers = new ArrayList<Officer>();

        Collections.max(officers, new Comparator<Officer>()
                {
                    public int compare( Officer a, Officer b )
                    {
                       return -1; //will do after
                    }
                }
            );
    }
}

因此,您发布的代码没有任何问题。一定是其他什么地方出了问题。

This compiles just fine. Here's an ideone.com demo

import java.util.*;

class Officer {
}

public class Test {

    public static void main(String[] args) {

        List<Officer> officers = new ArrayList<Officer>();

        Collections.max(officers, new Comparator<Officer>()
                {
                    public int compare( Officer a, Officer b )
                    {
                       return -1; //will do after
                    }
                }
            );
    }
}

So, nothing wrong with the code you posted. Must be something else that's wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文