排序方法和 toString 不起作用的多个问题

Multiple problems with sort method and toString not working

我的家庭作业有问题..我遇到的问题是:

我的排序方法有以下错误:

我的 toString 方法似乎也没有在控制台中输出任何信息。

这是家庭作业问题:

在本周的实验中,您将使用第 23 章中讨论的任何排序方法或选择排序对一组对象进行排序。这是你的选择。使用以下标准进行分配:

对象 class 应该是具有以下属性的学生:

id: 整数

名称:字符串

编写访问器、修改器、构造器和 toString()。

在您的主要测试中 class 您将编写主要方法并执行以下操作:

创建一个学生对象数组,数组中至少有 5 个学生。

sort方法必须自己写,包含在main中class。那种 方法将根据学生 ID 排序。

按存在的未排序顺序输出数组。

对数组进行排序

输出排序后的数组

这是我的主要内容CLASS:

public static void main(String[] args) {

    Student studentArray[] = new Student[5];

    for (int i=0; i < studentArray.length; i++) {
        studentArray[i] = new Student();
    }

    studentArray[0].id = 5555;
    studentArray[0].name = "Jim Jackson";
    studentArray[1].id = 4444;
    studentArray[1].name = "Craig Creedmoor";
    studentArray[2].id = 3333;
    studentArray[2].name = "Bill Biggums";
    studentArray[3].id = 2222;
    studentArray[3].name = "Frances Freeland";
    studentArray[4].id = 1111;
    studentArray[4].name = "Leslie Limerick";

    for (int i = 0; i<5; i++) {
        studentArray[i].toString();
    }

    sort(studentArray[]);

    for (int i = 0; i<5; i++) {
    studentArray[i].toString();
    }

}

public void sort(int[] studentArray) {
    for (int i = 1; i < studentArray.length; i++) {
        int currentElement = studentArray[i];
        int k;
        for (k = i -1; k >=0 && studentArray[k] > currentElement; k--) {
            studentArray[k + 1] = studentArray[k];
        }

        studentArray[k +1] = currentElement;
    }
}

这是我的学生CLASS

public int id;
public String name;

Student() {

}

public int getID() {
    return id;
}

public void setID(int i) {
    this.id = i;
}

public String getName() {
    return name;
}

public void setName(String n) {
    this.name = n;
}

public String toString() {
    return "The student's name is: " + this.name + "\n" +
           "The student's ID is: " + this.id;
}

所以根据作业说明,这是我从中得到的...

您的学生 class 中需要一个构造函数,您没有正确地将对象添加到数组中,您的排序方法也在访问数组中的元素 "Student" 并且您正在比较它键入 "int"。为了解决这个问题,我让学生数组中的对象实际访问了 ID。

另外...。您的排序方法似乎对我不起作用。说明说你可以使用选择排序,所以这就是我实现的。如果您有任何问题,请告诉我。

这应该有效,如果无效请告诉我,因为我不知道您的学生 class 在您的项目中是如何定义的。

public static void main(String[] args) {

        Student studentArray[] = new Student[5];

        studentArray[0] = new Student(5555, "Jim Jackson");
        studentArray[1] = new Student(4444, "Craig Creedmor");
        studentArray[2] = new Student(3333, "Bill Biggums");
        studentArray[3] = new Student(2222, "Frances Freeland");
        studentArray[4] = new Student(1111, "Leslie Limerick");

        sort(studentArray);

        for (int i = 0; i<5; i++) {
        System.out.println(studentArray[i].toString());
        }

    }

    public static void sort(Student[] arr) {
         for (int i = 0; i < arr.length - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < arr.length; j++)
                    if (arr[j].getID() < arr[index].getID()) 
                        index = j;

                int smallerNumber = arr[index].getID();
                String smallerString = arr[index].getName();
                arr[index].setID(arr[i].getID());
                arr[index].setName(arr[i].getName());
                arr[i].setID(smallerNumber);
                arr[i].setName(smallerString);
            }

}

然后是学生 class

public class Student {

private int id;
private String name;

public Student(int id, String name){
    this.id = id;
    this.name = name;
}

    public int getID() {
        return id;
    }

    public void setID(int i) {
        this.id = i;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String n) {
        this.name = n;
    }

    public String toString() {
        return "The student's name is: " + this.name + "\n" +
               "The student's ID is: " + this.id;
    }

}