这个问题在这里已经有答案了:
java根据字符串对自定义对象列表进行排序(7个答案)
26天前关门了。
我编写了一个工作方法,将对象插入sortedarraylist类中的ArrayList。问题是,它按arraylist第一个元素的第一个字母排序。
我希望能够选择arraylist的排序方式(例如,根据姓氏的第一个字母或用户对象中的书籍数量)。我该如何处理这个问题?
存储对象类型的示例:user(string firstname,string lasname,int books)
import java.io.*;
import java.util.Scanner;
public class Driver {
//These SortedArrayLists have been derived from the sorted arraylist class
public static SortedArrayList<User> sortedUsers = new SortedArrayList<>();
public static SortedArrayList<Book> sortedBooks = new SortedArrayList<>();
//This static method checks that input matches records and then sets the loaning user information
//for the book to be loaned while incrementing the number of books held by the user.
public static void loanBook(Book book, User user){
for (Book b : sortedBooks){
if(b.equals(book)) {
b.setLoanStatus(true);
b.setLoaningUser(user);
break;
}
}
for (User u: sortedUsers){
if(u.equals(user)){
u.setNumberOfBooks(u.getNumberOfBooks()+1); //The number of books of a given object is found then incremented by one to create the new value, which is set
break;
}
}
}
//This static method checks that input matches records and clears loaning user information
//for the book to be loaned while lowering the number of books held by the user by 1.
public static void returnBook(Book book, User user){
for (Book b : sortedBooks){
if(b.equals(book)){
b.setLoanStatus(false);
b.setLoaningUser(null);
break;
}
}
for (User u: sortedUsers){
if(u.equals(user)){
u.setNumberOfBooks(u.getNumberOfBooks()-1);
//The number of books for the object instance of a user in question is decreased since they have returned a book and thus have one less book.
}
}
}
//This is the main method from which the program starts.
public static void main(String[] args) throws FileNotFoundException, User.InvalidBookLimitException {
}
mainMenu(); //main menu printing method
char ch = sc.next().charAt(0);
sc.nextLine();
while (ch !='f') //the program ends as desired if f is pressed
{ switch(ch){
case 'b':
System.out.println("Displaying information about all books in the library: ");
//This toString replace method removes unwanted items for a cleaner print of book object information and removes the string description for user's name described in the user toString method.
System.out.println(sortedBooks.toString().replace("[","").replace("]","").replace("Name: ", ""));
break;
case 'u':
System.out.println("Displaying information about all users");
System.out.println(sortedUsers.toString().replace("[","").replace("]",""));
break;
case 'i':
System.out.println("Enter the loaning out data. ");
User user = readNames();
Book book = readBookName();
//A book object is created based on user input, then an attempt at altering the
// relevant object information is made via the loanBook method.
loanBook(book, user);
break;
case 'r':
System.out.println("Please the details of the book to be returned: ");
User userReturn = readNames();
Book bookReturn = readBookName();
//User input is used to create user and book objects so that a book can be returned
//by use of the returnBook method, resetting any user information about the book and decreasing the count for number of booksheld by the user.
returnBook(bookReturn, userReturn);
break;
default: //this case occurs if input does not match any of the switch statement cases.
System.out.println("Invalid input, please enter f, b, i or r");
}
mainMenu();
ch = sc.next().charAt(0);
sc.nextLine();
}
}
}
sortedarraylist类:import java.util.arraylist;
public class SortedArrayList<E extends Comparable<E>> extends ArrayList<E> {
//This insert method ensures that an object added to an arraylist is added in a sorted order.
public void insert(E value) {
if (this.size() == 0){
this.add(value);
return; }
for (int i = 0; i < this.size(); i++) {
int comparison = value.compareTo((E) this.get(i) );
if (comparison < 0) {
this.add(i, value);
return; }
if (comparison == 0){
return; }
}
this.add(value);
}
1条答案
按热度按时间yqhsw0fo1#
您可以简单地使用常规的arraylist(或任何类型的内置列表),在添加所有元素之后,使用
Collections.sort()
. 在这里,您可以使用列表作为第一个参数,使用自定义比较器作为第二个参数。对于自定义比较器,您可以提供所需的比较(姓氏等)。这种排序也比您当前使用的更有效。以下是javadoc:https://docs.oracle.com/javase/7/docs/api/java/util/collections.html#sort(java.util.list,%20java.util.comparator)