java - MyRoster 班级有一些问题。特别是我的排序方法和 removeNull 方法
问题描述
我正在尝试创建一个名为 roster 的类,它接受一个 Student 对象并且可以添加、删除、搜索等。我的所有方法都有效,除了在调用排序类时我不断收到 nullPointerException。此外,由于某种原因,当我加载名册时,它会两次打印数组中的最后一个元素。我认为这两个问题是相关的。
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 Student[] sort() { //O(n^2) runs n^2 times for the nested for loop
array = roster.toArray();
for(int i =0; i < array.length; i++) { // O(n)
for(int j =1; j < array.length - 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 (Student[]) array;
}
public void loadRoster() { // O(n) executes the length of the array
removeNull(array);
for(int i =0; i< array.length -1; 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();
//sort((Student[]) array);
counter++;
}
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--;
}
public Object[] removeNull(Object[] tempArr) {
int k = 0;
for(int i =0; i < array.length; i++) {
if (array[i] == null)
continue;
tempArr[k] = (Student) array[i];
k++;
}
return tempArr;
}
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]);
return (Student) array[i];
}
System.out.print("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
array = removeNull(array);
sort();
}
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]);
out.close();
}
}
解决方案
在您的排序函数中,您遍历整个学生数组,其中可能仍包含空值。请注意, array.length 将给出数组的大小而不是集合元素的数量。我建议你使用你的 removeNull() 方法来避免这个问题,或者在 for 循环中使用计数器值而不是 array.length。
推荐阅读
- aws-lambda - SNS 未触发 Lambda
- mysql - 为什么sql比较忽略大小写
- java - Java 无法处理 π 字符
- javascript - 为了继续使用 Tensorflow.js 进行预测,在 Vue.js 实例中使用无限循环是错误的吗?
- javascript - 从任何页面调用 jQuery 函数
- javascript - 在尝试抓取反应网站时获取 index.html 内容
- python - opencv 无法在 C++ 中从 ip 轴摄像头捕获视频,但在 Python 中有效
- html - 如何在swift中修复从html字符串转换NSAttributedString?
- java - 如何在折叠工具栏中设置导航箭头并设置 Onclicklistener
- r - 在 lme4 中指定随机效果