【发布时间】:2014-04-10 22:40:41
【问题描述】:
我无法将数组传递给我的排序类,因为我需要使用两种不同的算法对同一个数组进行排序。我收到错误
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;
}
编辑:我现在通过了,但他们只是通过零
【问题讨论】:
-
请查看数组、变量和方法调用语法。你认为这个
new Mergesort(int[] originalArray[] );做了什么?为什么? -
它希望我只放 originalArray 但只是将零传递给程序。
-
修复您的语法错误,以便您的程序能够编译。使用调试器查找程序错误。
标签: java constructor parameter-passing argument-passing