试图将数组传递给我的类构造函数

时间:2014-04-10 22:40:41

标签: java constructor parameter-passing argument-passing

我无法将数组传递给我的排序类,因为我需要使用两种不同的算法对相同的数组进行排序。我收到了错误

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;
}
编辑:我现在已经过去,但他们只是传递了零

2 个答案:

答案 0 :(得分:1)

将数组作为变量传递给函数和构造函数时,语法与传入普通变量的语法相同。

正确:

int[] myArray = new int[12];
myObject.myFunction(myArray);

不正确:

int[] myArray = new int[12];
myObject.myFunction(int[] myArray[]);

您可能想要查看this,它将帮助您在java中使用数组。

此外,您可能希望确保它们位于同一个包中。根据您缺少package my.package;,看起来您在默认包中有类。这很糟糕,因为你不能从单独的包中导入它们。 org.myamazingapplication中的类不能import myclass(假设myclass在默认包中)。此外,许多api例如bukkit都需要使用包。

答案 1 :(得分:0)

更改

Quicksort q = new Quicksort(int [] originalArray[]);
Mergesort m = new Mergesort(int[] originalArray[] );

Quicksort q = new Quicksort(originalArray);
Mergesort m = new Mergesort(originalArray);

并在两个构造函数内部,更改

for (int i = 0; i < n.length; i++) {
    n[i] = array[i];
}

for (int i = 0; i < n.length; i++) {
    array[i] = n[i];
}