首页 > 解决方案 > sort 方法只对前半部分进行排序

问题描述

我正在尝试对我的“学生”数组进行排序,但由于某种原因,它不会对我使用 addStudent 方法添加的学生进行排序,而只会对文本文件中的学生进行排序。另外,我数组中的最后一个元素被保存了两次,我也不知道为什么,有人对如何修复它有任何建议吗?

    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.LinkedList;
    
    public class Roster {
        
        private Object [] array = new Student [10];
        private LinkedList <Student> roster = new LinkedList<Student>();
        private int counter = 0;
        
        public Roster (Student student) {// O(1) this runs in constant time
            addStudent(student);
        }
    
        public Student[] getArray() {// O(1) this runs in constant time
            return (Student[]) array;
        }
    
        public void setArray(Student[] array) {// O(1) this runs in constant time
            this.array = array;
        }
    
        public LinkedList<Student> getRoster() { // O(1) this runs in constant time
            return roster;
        }
    
        public void setRoster(LinkedList<Student> roster) {// O(1) this runs in constant time
            this.roster = roster;
        }
        
        public Object[] sort() { //O(n^2) runs n^2 times for the nested for loop
            array = roster.toArray();
            removeNull();
                for(int i =0; i < counter; i++) { // O(n)
                    for(int j =1; j < counter - i; j++) // O(n)
                    if((((Student) array[j - 1]).compareTo((Student) array[j])) >= 1) {
                        Student temp = (Student) array[j-1];
                        array[j-1] = array[j];
                        array[j] = temp;
                }
            }
            return array;
        } 
    
        public void loadRoster() { // O(n) executes the length of the array
            removeNull();
            for(int i =0; i< array.length; i++)
                System.out.println(array[i]);
        }
        
        public void addStudent(Student student) { // O(n) toArray()
            if(counter == 10)
                throw new IllegalArgumentException("Sorry the class is full");
            roster.add(student);
            array = roster.toArray();
            counter++;
            loadRoster();
        }
        
        public void removeStudent(String ID) { // O(n) toArray()
            if(counter <= 0)
                throw new IllegalArgumentException("The class is empty");
            roster.remove(IDSearch(ID));            
            array = roster.toArray();
            counter--;
            loadRoster();
        }
        public void removeNull() {
            int k = 0;
            for(int i =0; i < counter; i++) {
                if (array[i] != null)
                    array[k++] = (Student) array[i];
            }   
        }
    
        public Student IDSearch( String IDNumber) {  // O(n) runs the for loop
            for(int i = 0; i < array.length;i++)
                if(((Student) array[i]).getIDNumber().equalsIgnoreCase(IDNumber)) {
                    System.out.print(array[i]);
                    System.out.println();
                    System.out.println();
                    return (Student) array[i];
                }
            
            System.out.println("Student not found");
            return null;
        }
        
        public Student nameSearch(String lastName , String firstName) { // O(n) for loop for array length
            for(int i = 0; i < array.length;i++)
                if((((Student) array[i]).getLastName().equalsIgnoreCase(lastName)) && (((Student) array[i]).getFirstName().equalsIgnoreCase(firstName))) {
                    System.out.println(array[i]);
                    return (Student) array[i];
                }
            System.out.print("Student not found");
            return null;    
        }
        
        public void save() { // constant time
            removeNull();
            sort();
            loadRoster();
        }
    
        public void saveChanges() throws FileNotFoundException{ // O(n) for loop to print to the new file
            save();
            File file = new File ("Roster.txt");
            
            if (file.exists())
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("Error");
                    e.printStackTrace();
                }
        
            PrintWriter out = new PrintWriter(file);
            for(int i =0; i <array.length; i++)
                out.println(array[i`enter code here`]);
            
            out.close();
        }
    }

标签: javaarrayssorting

解决方案


你似乎没有调用你的sort()方法。


推荐阅读