【问题标题】:removeNull() is causinng issues with my array删除 Null() 导致我的数组出现问题
【发布时间】:2020-07-15 20:10:16
【问题描述】:

我正在创建一个包含学生(对象)的花名册类。我应该将它们全部放在一个没有任何空元素的数组中,但是当我使用我的 removeNull() 方法时,它会在数组中添加最后一个 Student 两次。有谁知道我该如何解决这个问题?我认为它与 remove null 方法有关,因为当我不使用它时,我没有这个问题,除了我的数组充满了空值。 (不管有多少空值是最后一个学生被添加到数组中的次数)如果有人知道如何解决这个问题会很棒,我已经尝试了一切

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) {
            System.out.println("Sorry the class is full");
        }
        else {
            roster.add(student);
            array = roster.toArray();
            counter++;
            loadRoster();
            System.out.println("Student added");
        }
    }
    
    public void removeStudent(String ID) { // O(n) toArray()
        if(counter <= 0)
            System.out.println("The class is empty");
        else {
            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 ("studentList.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]);
        
        out.close();
    }
}

【问题讨论】:

    标签: java arrays sorting methods


    【解决方案1】:

    不是两次添加最后一个学生;相反,最后一个学生从未动过。你应该得到这样的行为:

    old list:  [s1,  s2,  null, s3,  s4 ]
    new list:  [s1,  s2,  s3,  s4,  (s4) ]
    

    当您删除 null 并适当移动时,列表的总大小不会改变。之后您需要适当地减小列表的大小。 顺便说一句,在此循环中无需转换为(Student);类型转换通常是您应该避免的,在这里它根本不会给您带来任何好处。

    【讨论】:

      【解决方案2】:

      既然您从列表中创建了数组,为什么不从列表中删除空值并重新创建数组?像这样的:

      roster.removeIf(Objects::isNull);
      array = roster.toArray();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 2012-11-01
        相关资源
        最近更新 更多