Why isn't Collections.binarySearch() working with this comparable?

前端 未结 2 1538
不思量自难忘°
不思量自难忘° 2021-01-27 01:00

I have this Player class which implements the Comparable interface. Then I have an ArrayList of Players. I\'m trying to use <

2条回答
  •  心在旅途
    2021-01-27 01:56

    Use are using generics inconsistently. Take heed of the compiler warnings. Always supply generic arguments (or never supply them).

    Instead of:

    class Player implements Comparable {
        [...]
        public int compareTo(Object o) {
    

    Use

    class Player implements Comparable {
        [...]
        public int compareTo(Player o) {
    

    The rules of generics are difficult enough without the complication of rare types. So, typically the language spec gives up if you mix them up.

提交回复
热议问题