Domanda

So I'm trying to sort an array (or vector. In my country it's called vector, but yeah) with a pretty easy algorithm. The language is Java and I'm using Eclipse. Nothing fancy, I know there are much more effective algorithms for sorting but that's not what I need now. Here's the code:

public void SortMethod(nbrs) {
int nbrs[];
int v[];

public void sort() {
    this.nbrs = nbrs;
    this.v=v;

    for (int i = 0; i < 10; i++) {
        int min = Integer.MIN_VALUE;
        int minIndex = -1;
        for (int k = i; k < 10; k++) {
            if (nbrs[k] < min) {
                min = nbrs[k];
                minIndex = k;
            }
        }
        v[minIndex] = v[i];
        v[i] = min;
    }
}

}

And in another project I have:

public class Vector {
public static void main(String[] args) {
    int nbrs[] = {2, 4, 67, 40, 32, 28, 9, 8, 55, 72 };

    nbrs.sort();

    for(int j = 0; j<10; j++){
        System.out.println(nbrs[j]);
    }
}
}

In the sorting method I get errors at the "void" part in

public void SortMethod(nbrs) {

at the "void" saying that @ is expected, and "Syntax error, insert Interface-identifier to complete InterfaceHeader.

Also I get error at

nbrs.sort();

In the Vector class.

Any help is appreciated.

È stato utile?

Soluzione

I'm sorry... you can't simply pass nbrs in the method (here, appropriately, constructor) signature. You need to declare what data type it is...

public class SortMethod {

    int[] nbrs;
    // ... the other fields.

    // This is a 'Constructor'
    public SortMethod (int[] nbrs) {
        this.nbrs = nbrs;
    }

Next, in your invocation of sort(), you should use something like:

// ...
SortMethod sMethod = new SortMethod(nbrs);
sMethod.sort();
// ...

More appropriately, you could create a static method that sorts the array, which implies no need for object creation for a trivial task:

public static void sort (int[] nbrs) {
    // ... code here.
}

and you can call it as:

// ...
<class_Name>.sort(nbrs); // from any where within the same package without an import.
// ...

TIP: You can try using the library method for your implementation:

// ...
java.util.Arrays.sort (nbrs);
// ...

Altri suggerimenti

public void SortMethod needs to be changed to

public class SortMethod {

     //provide getters and setters for nbrs if required
     public void sort(int[] nbrs) {
        ...
     }
}

And following changes are needed in Vector class:

public class Vector {
public static void main(String[] args) {
    int nbrs[] = {2, 4, 67, 40, 32, 28, 9, 8, 55, 72 };

    SortMethod sortNumbers = new SortMethod();
    sortNumbers.sort(nbrs);

    for(int j = 0; j<10; j++){
        System.out.println(nbrs[j]);
    }
}
}

Reworked your code a bit, so it actually works:

public class SortMethod {
    public static void sort(int[] nbrs) {
        for (int i = 0; i < 10; i++) {
            int min = Integer.MAX_VALUE;
            int minIndex = -1;
            for (int k = i; k < 10; k++) {
                if (nbrs[k] < min) {
                    min = nbrs[k];
                    minIndex = k;
                }
            }
            nbrs[minIndex] = nbrs[i];
            nbrs[i] = min;
        }
    }
}

public class Vector {
    public static void main(String[] args) {
        int nbrs[] = { 2, 4, 67, 40, 32, 28, 9, 8, 55, 72 };

        SortMethod.sort(nbrs);

        for (int j = 0; j < 10; j++) {
            System.out.println(nbrs[j]);
        }
    }
}

Use this

    import java.util.Arrays;

    Arrays.sort(nbrs);
System.out.println(Arrays.toString(nbrs));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top