Ожидается ".class" в Java - PullRequest
       7

Ожидается ".class" в Java

1 голос
/ 04 октября 2019

У меня ошибка при запуске кода на Java,

МОЙ КОД:

interface myFunc{
int func(int n[]);
}
class bubbleSort{
int bubble(int n[]){
    int result[];

    for(int j=0;j<n.length;j++){
        for(int i=j+1;i<n.length;i++){
            if(n[i] < n[j]){
                int t = n[j];
                n[j] = n[i];
                n[i] = t;
                result = t;
            }
        }
        return result;
    }
}
}
class test{
public static int lista(myFunc mf, int n){
    return mf.func(n[]);
}
public static void main(String[] args) {
    int intInt[] = {3,2,5,4,1};
    int intOut;

    bubbleSort sort = new bubbleSort();

    intOut = lista(sort::bubble, intInt[]);

    System.out.println(intOut);
}
}

МОЯ ОШИБКА:

test.java:23: error: '.class' expected
        return mf.func(n[]);
                          ^ test.java:31: error: '.class' expected
        intOut = lista(sort::bubble, intInt[]);
                                             ^ 2 errors error: compilation failed

Может кто-нибудь помочь мне исправитьэту проблему и объяснить?

1 Ответ

0 голосов
/ 04 октября 2019

Не уверен, что вы хотите, но по крайней мере это компилируется. Возвращает максимум массива или -1, если массив пустой / нулевой.

Не уверен, ищете ли вы этот или отсортированный массив.

Как сказал Слав, большинство проблем заключалось в синтаксисе и несоответствии между int [] и int.

interface myFunc{

    int func(int n[]);

}

class bubbleSort {

    int bubble(int n[]){
        // int result = 0;

        if(n == null || n.length == 0){
            return -1;
        }

        for(int j=0;j<n.length;j++){
            for(int i=j+1;i<n.length;i++){
                if(n[i] > n[j]){
                    int t = n[j];
                    n[j] = n[i];
                    n[i] = t;
                   // result = t; // You dont need this... i think...
                }
            }

        }

        return n[0];
    }
}

class test {

    public static int lista(myFunc mf, int[] n){
        return mf.func(n);
    }

    public static void main(String[] args) {
        int intInt[] = {4,3,1,20,3,6,10};
        int intOut;

        bubbleSort sort = new bubbleSort();

        intOut = lista(sort::bubble, intInt);

        System.out.println(intOut);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...