【发布时间】: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