请考虑以下代码:
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的非法修饰符;只允许final。
X 返回 B;
错误说:void 方法不能返回值。我已经将方法返回类型定义为整数数组,那么为什么会出现此错误?
X 左 = 合并排序(左);X 右 = 合并排序(右);
错误说:类型 MergeSort 的方法合并排序未定义