我无法将数组传递给我的排序类,因为我需要使用两种不同的算法对相同的数组进行排序。我收到错误尝试将数组传递给我的类构造函数
Multiple markers at this line
- Syntax error on token(s), misplaced
construct(s)
- Syntax error on token(s), misplaced
construct(s)
- The constructor Mergesort() is undefined
- Syntax error on token "originalArray", delete
this token
代码
class sorterProgram {
public static void main(String args[]) {
//Declares instances of the sorting classes
int[] originalArray = new int[500];
for (int i = 0; i < 500; i++) {
originalArray[i] = (int) Math.round(Math.random() * 100);
}
Quicksort q = new Quicksort(originalArray);
Mergesort m = new Mergesort(originalArray);
//declares keyboard to accept user input for type of sort
Scanner keyboard = new Scanner(System.in);
//choice set as one so the do-while and if statements will start
int choice = 1;
// loop that does sorting untill the user is done
do {
System.out.println("Enter the # to start the sort of a 500 Element Array: \n1: Quicksort then Mergesort \n2: Exit");
//only works if the user chooses the correct numbers
if (1 == choice || choice == 2) {
choice = keyboard.nextInt();
}
switch (choice) {
case 1:
System.out.println("Before Quicksort");
q.print();
long timeQuicksort = System.nanoTime();
q.quicksort();
long completedInQuicksort = System.nanoTime() - timeQuicksort;
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("After Quicksort ");
q.print();
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Before Mergesort");
m.print();
long timeMergesort = System.nanoTime();
m.sort();
long completedInMergesort = System.nanoTime() - timeMergesort;
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("After Mergesort ");
m.print();
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Time took to complete Quicksort (nanoseconds): "+ completedInQuicksort);
System.out.println("Time took to complete Mergesort (nanoseconds): "+ completedInMergesort);
break;
case 2:
System.out.println("Thanks for using the Quicksort and Mergesort");
}
} while (choice != 2);
}
}
,这些都是我的两个分类器构造
class Quicksort {
int array[];
int size;
public Quicksort(int[] n) {
size = n.length;
// create array for merge sorting with size n+1
array = new int[n.length + 1];
// assign value into the array
for (int i = 0; i < n.length; i++) {
n[i] = array[i];
}
class Mergesort {
private int size;
private int[] array;
private int[] tempMergeArray;
public Mergesort(int[] n) {
size = n.length;
// create array for merge sorting with size n+1
array = new int[n.length + 1];
// assign value into the array
for (int i = 0; i < n.length; i++) {
n[i] = array[i];
}
// set the last value as a big value so the sorting ends properly
array[n.length] = 99999;
}
编辑:我现在路过,但他们只是路过零
请检查数组,变量和方法调用语法。你认为这个新的Mergesort(int [] originalArray []);'做了什么,为什么? –
它希望我放入原始数组,但只是将零传入程序。 –
修复您的语法错误,以便您的程序编译。使用调试器来查找程序错误。 –