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