java中的类 - 如何打印所有数组以及排序后的所有详细信息

时间:2016-06-11 14:49:17

标签: java arrays sorting

我的运动有问题,我尝试按Num Of Times Pen Falls对所有讲师的数组进行排序。

我把所有Num Of Times Pen将每个讲师放在一个临时数组中,然后使用函数对数组进行排序,然后打印出一个排序数组allLecturers。

如何在排序数组后打印所显示的allLecturers排序?

这是功能:

public void sortingEmployee() {
    int[] arr = new int[allLecturers.length];

    for (int i = 0; i < allLecturers.length; i++) {
        if (allLecturers[i] == null)
            break;
        arr[i] = allLecturers[i].getNumOfTimesPenFalls();

    }
    selectionSort(arr);

 }

大学课程:

public class College extends Lecturer {
    private String name;
    private int numOfLecturers;
    public Lecturer[] allLecturers;
    private int index;

    public College(String name, int numOfLecturers, int MaxLecturers) {
        this.name = name;
        this.numOfLecturers = numOfLecturers;
        this.allLecturers = new Lecturer[MaxLecturers];
    }

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

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

    public void setNumOfLecturers(int numOfLecturers) {
        this.numOfLecturers = numOfLecturers;
    }

    public int getNumOfLecturers() {

        return this.numOfLecturers;
    }

    public void setAllLecturers(Lecturer[] allLecturers) {
        this.allLecturers = allLecturers;
    }

    public Lecturer[] getAllLecturers() {
        return this.allLecturers;
    }

    public void addEmployee(String name, int numOfTimesPenFalls, String favoriteIceCream, int automaticNum) {

        allLecturers[index] = new Lecturer(name, numOfTimesPenFalls, favoriteIceCream, automaticNum);
        index++;
    }

    public void sortingEmployee() {
        int[] arr = new int[allLecturers.length];

        for (int i = 0; i < allLecturers.length; i++) {
            if (allLecturers[i] == null) break;
            arr[i] = allLecturers[i].getNumOfTimesPenFalls();

        }
        selectionSort(arr);

    }

    public void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            if (i != min) {
                int swap = arr[i];
                arr[i] = arr[min];
                arr[min] = swap;
            }
        }
    }

    public void print() {
        for (int i = 0; i < allLecturers.length; i++) {
            if (allLecturers[i] == null) break;
            System.out.println(allLecturers[i]);
        }

    }
}

讲师班:

public class Lecturer {
    private String name;
    private int numOfTimesPenFalls;
    private String favoriteIceCream;
    private int automaticNum;

    public Lecturer() {

    }

    public Lecturer(String name, int numOfTimesPenFalls, String favoriteIceCream, int automaticNum) {
        this.name = name;
        this.numOfTimesPenFalls = numOfTimesPenFalls;
        this.favoriteIceCream = favoriteIceCream;
        this.automaticNum = automaticNum;
    }

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

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

    public void setNumOfTimesPenFalls(int numOfTimesPenFalls) {

        this.numOfTimesPenFalls = numOfTimesPenFalls;
    }

    public int getNumOfTimesPenFalls() {
        return this.numOfTimesPenFalls;
    }

    public void setFavoriteIceCream(String favoriteIceCream) {
        this.favoriteIceCream = favoriteIceCream;
    }
    public String getFavoriteIceCream() {
        return this.favoriteIceCream;
    }

    public void setAutomaticNum(int automaticNum) {
        this.automaticNum = automaticNum;
    }

    public int getAutomaticNum() {
        return this.automaticNum;
    }

    public String toString() {
        return "NAME: " + this.name + " ,num Of Times Pen Falls: " + this.numOfTimesPenFalls + " ,favoriteIceCream: " + this.favoriteIceCream + " ,Automatic Number: " + this.automaticNum;
    }
}

主要课程:

public class main {
    public static void main(String[] args) {

        College c1 = new College("College1", 3, 4);

        Lecturer L1 = new Lecturer("david", 5, "apple", 1001);
        Lecturer L2 = new Lecturer("yosi", 5, "apple", 1001);

        c1.addEmployee("yosi", 5, "apple", 1001);
        c1.addEmployee("david", 3, "apple", 1001);
        c1.addEmployee("liran", 8, "apple", 1001);

        c1.print();
    }
}

0 个答案:

没有答案