Comparator<MedicalShifts> 类型的方法 compare(MedicalShifts, MedicalShifts) 不适用于参数 (P, P)

The method compare(MedicalShifts, MedicalShifts) in the type Comparator<MedicalShifts> is not applicable for the arguments (P, P)

我试图在通用数组上使用比较器,但出现以下错误

The method compare(MedicalShifts, MedicalShifts) in the type Comparator is not applicable for the arguments (P, P)Java(67108979)

Main.java

public class Main {
    public static void main(String[] args) throws EmptyQueueException {
        Queue<MedicalShifts> queueHospital = new QueueImplement<>();
        queueHospital.add(new MedicalShifts("harry", 0));
        queueHospital.add(new MedicalShifts("hermione", 3));
        queueHospital.add(new MedicalShifts("ron", 1));
        queueHospital.add(new MedicalShifts("luna", 5));
        queueHospital.add(new MedicalShifts("voldemort", 8));
        System.out.println(queueHospital.peek());
        System.out.println(queueHospital.isEmpty());
        try {
            System.out.println(queueHospital.pop());
        } catch (EmptyQueueException emptyQueue) {
            System.out.println(emptyQueue.getMessage());
        }
        System.out.println(queueHospital.peek());
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        queueHospital.add(new MedicalShifts("hagrid", 4));
        for (MedicalShifts shifts : queueHospital) {
            System.out.println(shifts);
        }
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        queueHospital.sort();
        for (MedicalShifts shifts : queueHospital) {
            System.out.println(shifts);
        }
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        System.out.println(queueHospital.min(new Comparator<MedicalShifts>() {
            @Override
            public int compare(MedicalShifts o1, MedicalShifts o2) {
                return o1.getNombre().compareTo(o2.getNombre());
            }
        }));
    }
}

QueueImplement.java

public class QueueImplement<P extends Comparable<P>> implements Queue<P> {
  private P[] elements;
  private static int INITIAL_DIM = 10;
  private int cantidad = 0;

  public QueueImplement() {
    P[] elements = (P[]) new Comparable[INITIAL_DIM]; // Creación de un array de elementos genéricos comparables
    this.elements = elements;
  }

  @Override
  public boolean add(P persona) throws EmptyQueueException {
    if (cantidad == elements.length) {
      throw new EmptyQueueException();
    }
    elements[cantidad++] = persona;
    return true;
  }

  @Override
  public P peek() {
    return elements[0];
  }

  @Override
  public P pop() throws EmptyQueueException {
    P first = elements[0];
    this.elements = Arrays.copyOfRange(elements, 1, elements.length); // si hay error, es - 1
    cantidad--;
    return first;
  }

  @Override
  public P min(Comparator<MedicalShifts> comp) {
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (comp.compare(elements[j], elements[j+1]) > 0) {
                  P temp = elements[j];
                  elements[j] = elements[j+1];
                  elements[j+1] = temp;
             }
         }
    }
    return elements[0];
  }

  @Override
  public P max() { // devuelve el nombre mas grandajo. Se puede sortear y dsp agarrar el ultimo
    return elements[cantidad - 1];
  }

  @Override
  public void sort() {
    Arrays.sort(elements, 0, cantidad);
  }

  @Override
  public Iterator<P> iterator() {
    return new Iterator<P>() {
      private int index = 0;

      @Override
      public boolean hasNext() {
        return index < cantidad;
      }

      @Override
      public P next() {
        return elements[index++];
      }
    };
  }

  @Override
  public boolean isEmpty() {
    return cantidad == 0;
  }
}

错误发生在 min 函数的 comp.compare 处。所有其他方法都可以与泛型一起完美工作,所以我不知道为什么它不能与比较器具有的比较功能一起工作。

您混合了通用类型和特定类型。您的 minP 上应该是通用的(而不是在 MedicalShifts 上特定)。你想要

@Override
public P min(Comparator<P> comp) {
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
            if (comp.compare(elements[j], elements[j+1]) > 0) {
                P temp = elements[j];
                elements[j] = elements[j+1];
                elements[j+1] = temp;
            }
        }
    }
    return elements[0];
}

我不知道 min 函数中的排序。也许

@Override
public P min(Comparator<P> comp) {
    P m = null;
    for (int i = 0; i < n; i++){
        if (m == null || comp.compare(m, elements[i]) < 0) {
            m = elements[i];
        }
    }
    return m;
}

你不会是我的学生吧?我已经想在口头上见到你 :))))))