java - Java 中应有“.class”

标签 java sorting compiler-errors

当我在 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

谁能帮我解决这个问题并解释一下吗?

最佳答案

不确定你想要什么,但至少这个是可以编译的。 返回数组的最大值,如果数组为空/null,则返回 -1。

不确定您是在寻找这个数组还是已排序的数组。

正如 Slaw 所说,大多数问题是语法以及 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);
    }
}

关于java - Java 中应有“.class”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58227383/

相关文章:

java - 如何在 Freemarker 列表之间获取逗号

java - 确定方法调用是否在多个线程上执行

java - 有没有办法在不访问应用程序代码的情况下中断正在运行的 JVM 中的 hibernate 线程?

python - 使用长度和字母顺序对列表进行排序

c# - 自建的Node <T>\List <T>类到系统节点和列表之间的C#模糊引用

c++ - Bison 和用户类 : compiling error

java - Spring Security 在尝试注销时返回 302

c - 按降序对数组中的奇数进行排序

python - 删除文本文件中的非重复行

c - 条件表达式: different behavior between compilers中的指针类型不匹配