Для трех классов X, Y и Z в Java.java Компараторы для класса в другом классе
В X У меня есть общественная нестатическая функция «двойное расстояние (Y)». В Z У меня есть объект x типа X и массив. Как я могу отсортировать массив в порядке возрастания расстояния() от x?
Я пытаюсь найти решение, используя компаратор для Y, но я должен признать, что я не знаю. Спасибо!
Редактировать: У меня все еще есть ошибка, о которой я больше не мог найти. На основе решения в ответ, мой код будет выглядеть так:
public SARAgent(PathBandit game, int n) {
...
Arrays.sort(getEdgeSet(game).toArray(), BY_GAP);
}
public static Comparator<Edge> BY_GAP = new Comparator<Edge>() {
public int compare(Edge a, Edge b) {
double va = game.delta(a.getX(), a.getY(), a.getDir());
double vb = game.delta(b.getX(), b.getY(), b.getDir());
if (va == vb) return a.compareTo(b);
if (va < vb) return -1;
if (va > vb) return +1;
return 0;
}
};
Края Сопоставимые, но код не компилируется с ошибкой на вызове Arrays.sort(). Ошибка
Error: no suitable method found for sort(java.lang.Object[],java.util.Comparator<Edge>)
method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
(actual argument java.util.Comparator<Edge> cannot be converted to java.util.Comparator<? super java.lang.Object> by method invocation conversion)
method java.util.Arrays.sort(java.lang.Object[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(java.lang.Object[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(double[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(double[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(float[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(float[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(byte[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(char[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(short[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(short[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(long[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(long[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(int[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(int[]) is not applicable
(actual and formal argument lists differ in length)
Описание ок. . где есть код? –
Извините, я не знал, что должен опубликовать код. Вопрос был прост, и код был сбит с толку и, вероятно, концептуально неправильный ... – user2315499