开发者

Collections.max() Improper Arguments

开发者 https://www.devze.com 2023-02-18 17:15 出处:网络
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

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消