我为我的库应用程序设置了一个类型为book的ArrayList。我试图实现的当前功能是编辑书的细节。我有一个名为bookID
的变量,所以当我通过ArrayList调用此方法时,它将是newBook.get(index).getBookID();
。有了这些信息,我想查看,并做以下事情:
我面临的问题:
到目前为止我想出了什么:
// Editing book in ArrayList
public void editBook(){
System.out.println("==============================");
// Ensuring array has at least one book
if(newBook.size() > 0){
// Get which part, and book the user would like to edits
System.out.print("Enter Book ID to begin editing: ");
int bookID = sc.nextInt();
System.out.println("Press 1 or 2 to edit.");
System.out.println("1: Edit title");
System.out.println("2: Edit author");
System.out.print("Enter option: ");
int editOption = sc.nextInt();
// Switch statement which will handle editing book
switch(editOption){
case 1:
// New title
System.out.print("Enter new title: ");
String newTitle = sc.nextLine();
sc.next();
// Updates title
newBook.get(position).setTitle(newTitle);
// Prints out title
System.out.println(newBook.get(position).getTitle());
break; // Edit title
以上代码只是部分代码,低于此代码的任何内容都与问题无关。
答案 0 :(得分:1)
所有这一切都会循环遍历List
Book
,并将用户输入的bookID
与图书的bookID
进行比较。如果找到匹配项,则Book
引用将保留在变量bookToEdit
中。
如果在循环完成后,bookToEdit
为null
,那么Book
中的List
与指定的ID不匹配,否则,您现在拥有参考需要编辑的书
// Get which part, and book the user would like to edits
System.out.print("Enter Book ID to begin editing: ");
int bookID = sc.nextInt();
sc.nextLine();
Book bookToEdit = null;
for (Book book : newBook) {
if (book.getId() == bookID) {
bookToEdit = book;
break;
}
}
if (bookToEdit != null) {
System.out.println("Press 1 or 2 to edit.");
System.out.println("1: Edit title");
System.out.println("2: Edit author");
System.out.print("Enter option: ");
int editOption = sc.nextInt();
sc.nextLine();
if (editOption == 1 || editOption == 2) {
System.out.print("New " + (editOption == 1 ? "title: " : "author: "));
String value = sc.nextLine();
switch (editOption) {
case 1:
// Update title
break;
case 2:
// Update author
break;
}
} else {
System.out.println("Invalid edit option");
}
} else {
System.out.println("Book with the id of " + bookID + " does not exist");
}
Stream
Š如果你想做一些更有趣的事情,可以使用......
// Get which part, and book the user would like to edits
System.out.print("Enter Book ID to begin editing: ");
int bookID = sc.nextInt();
sc.nextLine();
List<Book> filtered = newBook.stream().filter((Book t) -> t.getId() == bookID).collect(Collectors.toList());
if (!filtered.isEmpty()) {
Book bookToEdit = filtered.get(0);
现在,您需要知道,这比前一个循环效率低,因为它将遍历整个List
newBook
,并将返回所有匹配bookId
的书籍{1}}(实际上应该只有一个)
Map
最有效的方法是将Book
保存在Map
中,而不是List
,并键入bookId
。这样,在需要时,您可以通过它的id
Map<Integer, Book> newBook = new HashMap<>();
//...
newBook.put(1, new Book(1, "Star Wars", "Somebody"));
newBook.put(2, new Book(2, "Harry Potter", "Somebody else"));
//...
// Get which part, and book the user would like to edits
System.out.print("Enter Book ID to begin editing: ");
int bookID = sc.nextInt();
sc.nextLine();
if (newBook.containsKey(bookID)) {
Book bookToEdit = newBook.get(bookID);
从概念上讲,所有这一切都是在密钥和对象之间生成映射或关系,这使得根据提供的密钥更快更简单地找到对象
有关详细信息,请查看Collections Trail
答案 1 :(得分:0)