java - 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();
}
}
解决方案
你似乎没有调用你的sort()
方法。
推荐阅读
- excel - MS-Word、VBA:将存储在图像控件中的图片以表格形式复制到文档中
- c# - 在不调试的情况下运行包含 .NET 微服务的 docker 容器
- c# - 使用 Visual Studio 2019 打开时项目被卸载
- plugins - 如何在 Word 插件中添加评论作为链接?
- reactjs - 如何解决使用 this.refs 已被弃用?
- r - 按值范围对数据框进行子集
- node.js - 在 node.js 项目中安装 NuGet 包
- php - WooCommerce 产品变体:自动启用管理库存并设置库存数量
- excel - 在 vb.net 中创建 excel 对象的最佳方法
- rest - 邮递员响应正文中的电子邮件 ID 转换为小写字母