我的程序看不到它的主要方法。我不明白为什么?
我有一个main方法,但在控制台中它说:“错误:无法找到或加载主类mergesortmini.MergeSortMini”。
如果相关的话,我将MergeSort类从我的另一个项目中复制过来。
public class MergeSortMini {
public static void main(String[] args) {
MergeSort m = new MergeSort();
m.init().sort().printData();
}
}
class MergeSort {
int[] data;
public MergeSort init() {
data = new int[10];
data[0] = 5;
data[1] = 67;
data[2] = 0 - 12;
data[3] = 7;
data[4] = 1;
data[5] = 2;
data[6] = 99;
data[7] = 3;
data[8] = 4;
data[9] = 67;
return this;
}
public MergeSort sort() {
data = this.mergeSort(data);
return this;
}
public int printData() {
int i = 0;
while(i < data.length){
System.out.println(data[i]);
i = i + 1;
}
return data.length;
}
public int[] merge(int[] a1, int[] a2) {
int lengthOfRes = a1.length + a2.length;
int resArr[] = new int [lengthOfRes];
int a1Index = 0;
int a2Index = 0;
int resIndex = 0;
while((a1Index < a1.length) || (a2Index < a2.length))
{
if((a1Index < a1.length) && (a2Index < a2.length)){
if(a1[a1Index] < a2[a2Index]){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
} else
{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}
else if(a1Index < a1.length){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
}
else
{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}
return resArr;
}
public int[] mergeSort(int[] arr) {
if (arr.length < 2){
return arr;
}
int mid = arr.length / 2;
int leftArr[] = new int [mid];
int rightArr[];
int halfLength = arr.length;
if(isEven(halfLength))
{
rightArr = new int [mid];
}
else
{
rightArr = new int[mid + 1];
}
int resultArr[] = new int [arr.length];
int i = 0;
while(i < mid){
leftArr[i] = arr[i];
i = i + 1;
}
int j = mid;
int indexOfRight = 0;
while(j < arr.length){
rightArr[indexOfRight] = arr[j];
indexOfRight = indexOfRight + 1;
j = j + 1;
}
leftArr = mergeSort(leftArr);
rightArr = mergeSort(rightArr);
return resultArr = merge(leftArr, rightArr);
}
public boolean isEven(int number){
return (!((number - (number * 0.5) * 2) > 0)) && (!((number - (number * 0.5) * 2) < 0));
}
}
任何帮助都会受到大力赞赏!
答案 0 :(得分:2)
确保MergeSortMini
有包裹声明
package mergesortmini;