1
 class pair{
    int start;
    int end;
    pair(int start, int end)
    {
        this.start = start;
        this.end = end;
    }
}
public class Solution {
    public void intervals(ArrayList<Integer> arrive, ArrayList<Integer> depart) {
        ArrayList<pair> list = new ArrayList<>();
        for(int i=0;i<arrive.size();i++)
        {
            pair p = new pair(arrive.get(i),depart.get(i));
            list.add(p);
        }
        Collections.sort(list, new Comparator()
        {
            @Override
            public int compare(pair a, pair b)
            {
               return a.start.compareTo(b.start); 
            }
            });

The error says that a.start is not allowed here. but compareTo should work fine for interger values.

    ./Solution.java:19: error: <anonymous Solution$1> is not abstract and does not override abstract method compare(Object,Object) in Comparator
        {
        ^
./Solution.java:20: error: method does not override or implement a method from a supertype
            @Override
            ^
./Solution.java:23: error: int cannot be dereferenced
               return (a.start).compareTo(b.start); 
                               ^
Note: ./Solution.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors

the Override looks correct to me. I dont know why it's giving error.

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
  • Should be `new Comparator()`. And if `start` is an int, you can't call a method on it, because it's not an object. – khelwood Aug 10 '20 at 07:29
  • Unrelated: please follow java naming conventions. Class names go UpperCase, always. So, it should be `Comparator` ;-) – GhostCat Aug 10 '20 at 07:31

1 Answers1

0

this here is the reason:

return a.start.compareTo(b.start); 

since a is defined as integer, compareTo is invalid, int is a primitive an not an object, so there is no method compareTo defined for those in java...

you can either

compare those integers using the proper operator (<< >> <= >=) or replace them for its wrappers Integer

class pair{
    Integer start;
    Integer end;
ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83