SortedArrayList<E extends Comparable<? super E>> extends ArrayList<E> {
public SortedArrayList(){
}
public void insertionSort(E a){
for (int i=1; i<this.size(); i++ )
{
E value = this.get(i);
int j;
for (j = i; j > 0; j--)
{
if (this.get(j - 1).compareTo(value)<0) {
break;
}
else {
this.set(j, this.get(j-1));
//a.set(j-1, a.get(j));
}
}
this.set(j, value);
}
}
}
我有上面的自定义插入排序方法。
需要对以下类进行排序,如下所示:
public class User implements Comparable{
private String firstname;
private String surname;
private int numberOfBooksHeld;
@Override
public int compareTo(Object o) {
int LnComp = surname.compareTo(this.surname);
if (LnComp != 0) {
return LnComp;
}
else {
int FnComp = firstname.compareTo(this.firstname);
return FnComp;
}
}
和
public class Book implements Comparable {
private String title;
private String authorFirstName;
private String authorSurname;
private boolean loan;
private User user;
@Override
public int compareTo(Object o) {
int LnComp = authorSurname.compareTo(this.authorSurname);
return LnComp;
}
我在一个叫做“图书馆”的班上讲分类表的故事
import java.util.ArrayList;
public class Library {
private final ArrayList<Book> totalBookInventory = new ArrayList<>();
private ArrayList<Book> booksOnLoan = new ArrayList<>();
private ArrayList<Book> booksInStock = new ArrayList<>();
private ArrayList<User> userList = new ArrayList<>();
...}
totalbookinventory是排序的arraylist,其中存储排序的图书,userlist是存储排序的用户列表
我正在努力从我的驱动程序类调用它
我试过使用:
import java.io.*;
导入java.util.arraylist;导入java.util.scanner;
public class Driver {
private final Library l;
private final User u;
private final SortedArrayList s1;
public Driver() {
this.l = new Library();
this.u = new User();
this.s1 = new SortedArrayList();
}
我正在努力从driver类向insertionsort方法添加/调用新的用户/书籍,然后将其添加到totalbookinventory数组和userlist中。
我试过:
s1.insertionSort(new User(splitString[0], splitString[1], 0));
但是,这显然不会添加到用户列表中。
bookList.insertionSort(new User(splitString[0], splitString[1], 0));
而且这个代码不会访问insertionsort类。
在访问和添加必要的arraylist时遇到了困难,我需要将arraylists作为driver字段还是?请帮忙,提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!