Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试对java中的数字数组进行排序_Java_Sorting_Vector_Syntax_Void - Fatal编程技术网

尝试对java中的数字数组进行排序

尝试对java中的数字数组进行排序,java,sorting,vector,syntax,void,Java,Sorting,Vector,Syntax,Void,所以我试着用一种非常简单的算法对数组(或向量)进行排序,在我的国家它被称为向量,但是是的。语言是Java,我正在使用Eclipse。没什么特别的,我知道有更有效的排序算法,但这不是我现在需要的。代码如下: public void SortMethod(nbrs) { int nbrs[]; int v[]; public void sort() { this.nbrs = nbrs; this.v=v; for (int i = 0; i < 10; i++)

所以我试着用一种非常简单的算法对数组(或向量)进行排序,在我的国家它被称为向量,但是是的。语言是Java,我正在使用Eclipse。没什么特别的,我知道有更有效的排序算法,但这不是我现在需要的。代码如下:

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;
    }
}

}
在“void”表示应为@和“语法错误”处,插入接口标识符以完成InterfaceHeader

我还得到了错误在

nbrs.sort();
在向量类中


非常感谢您的帮助。

很抱歉…您不能简单地在方法(这里,适当地,构造函数)签名中传递
NBR
。您需要声明它是什么数据类型

public class SortMethod {

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

    // This is a 'Constructor'
    public SortMethod (int[] nbrs) {
        this.nbrs = nbrs;
    }
接下来,在调用
sort()
时,应该使用以下命令:

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

更合适的是,您可以创建一个对数组排序的
静态方法,这意味着不需要为一个简单的任务创建对象

public static void sort (int[] nbrs) {
    // ... code here.
}
你可以称之为:

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

公共无效排序方法
需要更改为

public class SortMethod {

     //provide getters and setters for nbrs if required
     public void sort(int[] nbrs) {
        ...
     }
}
Vector类中需要进行以下更改:

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]);
    }
}
}
公共类向量{
公共静态void main(字符串[]args){
int NBR[]={2,4,67,40,32,28,9,8,55,72};
SortMethod SortNumber=新的SortMethod();
分类号(NBR);

for(int j=0;j稍微修改了代码,因此它实际上可以工作:

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;
        }
    }
}
公共类排序方法{
公共静态无效排序(int[]nbrs){
对于(int i=0;i<10;i++){
int min=整数最大值;
int minIndex=-1;
对于(int k=i;k<10;k++){
如果(丁腈橡胶[k]

公共类向量{
公共静态void main(字符串[]args){
int NBR[]={2,4,67,40,32,28,9,8,55,72};
排序方法(nbrs);
对于(int j=0;j<10;j++){
系统输出打印LN(nbrs[j]);
}
}
}
使用此

    import java.util.Arrays;

    Arrays.sort(nbrs);
System.out.println(Arrays.toString(nbrs));

你的语法不正确。你应该声明SortMethod类的变量,然后使用它的方法。你的代码中有很多错误。我认为你应该首先彻底了解Java中类、对象、方法和方法参数是如何工作的,然后再继续:是的,我现在可以看到了。现在已经向我指出了错误很明显。谢谢。谢谢你提供了全面的教学性答案。因此,由于
公共静态无效
我不需要创建参数对象?与
公共无效
相反?@user3535325是的……我建议你下载。我知道该方法,但我也希望通过这种方式来学习。不过,谢谢嗯,这种排序方法叫做“选择排序”()。
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]);
    }
}
}
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]);
        }
    }
}
    import java.util.Arrays;

    Arrays.sort(nbrs);
System.out.println(Arrays.toString(nbrs));