请考虑以下代码:
public class MergeSort {
public static void main(String[] args) {
int[] A = new int[10];
// Printing the Initial Arrays
System.out.println("Array Elements before Sorting are as follows:");
for(int i = 0;i < A.length;i++){
System.out.println(A[i] + " ");
}
// Call the MergeSort Method here
// Printing Sorted Array here
System.out.println("Sorted Array are as follows:");
//for(int j = 0;j < result.length;i++){
//System.out.println(result[i] + " ");
//}
public static int[] merge_sort(int[] B){
if(B.length <=1){
return B;
}
int midpoint = B.length/2;
int[] left = new int[midpoint];
int[] right;
if(B.length % 2 == 0){
right = new int[midpoint];
}else {
right = new int[midpoint+1];
}
// An Extra Array to store the result
int[] result = new int[B.length];
// Populating the array in the left array
for (int i = 0; i < midpoint; i++){
left[i] = B[i];
}
// Populating in the right array
int x = 0;
for (int j=midpoint;j<B.length;j++){
right[x] = B[j];
x++;
}
// Using recursion to divide the array in left and right again and again
left = merge_sort(left);
right = merge_sort(right);
}// END OF METHOD merge_sort
}// END MAIN METHOD
}// END OF CLASS MergeSort
我在Eclipse中的以下行中收到以下错误:十字标志表示 Eclipse中的错误点:
X public static int [] merge_sort(int [] B){
错误说:参数merge_sort的非法修饰符;只允许决赛。
X返回B;
错误说:void方法无法返回值。我已经将方法返回类型定义为 整数数组那么为什么我会收到这个错误?
X left = merge_sort(左); X right = merge_sort(右);
错误说明:对于类型MergeSort
,方法合并排序未定义答案 0 :(得分:4)
我认为如果您修复代码中的缩进,问题就会很明显。您的merge_sort
方法中已声明main
方法。
只需将}// END MAIN METHOD
右括号移动到您声明merge_sort
的上方的行上,它就可以编译好了。
答案 1 :(得分:1)
你不能在另一种方法中使用方法!
您的merge_sort()
方法位于main
方法内!