【问题标题】:sort method only sorts first halfsort 方法只对前半部分进行排序
【发布时间】:2020-07-15 13:49:21
【问题描述】:

我正在尝试对我的“学生”数组进行排序,但由于某种原因,它不会对我使用 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();
        }
    }

【问题讨论】:

  • 插入任何学生后,您必须调用 sort()。
  • 我觉得你应该看看你的 removeNull 方法,用于最后一个学生的双重输入。
  • 我知道这是它的来源,因为当我不称它时,名称打印正常,但我不确定是什么原因造成的。你有什么想法吗?

标签: java arrays sorting


【解决方案1】:

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

【讨论】:

  • 我在我的主函数中调用它,但我没有把它放出来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2015-01-20
  • 2019-04-05
  • 2018-05-21
  • 2020-09-12
相关资源
最近更新 更多