gpt4 book ai didi

java - 仅当 ArrayList 等于 bookID 时才更新它吗?

转载 作者:行者123 更新时间:2023-11-30 07:44:17 25 4
gpt4 key购买 nike

我已经为我的图书馆应用程序设置了一个 Book 类型的 ArrayList。我当前尝试实现的功能是编辑一本书的详细信息。我有一个名为 bookID 的变量,因此当我通过 ArrayList 调用此方法时,它将是 newBook.get(index).getBookID();。根据这些信息,我想检查并执行以下操作:

  • 数组中存在具有此 ID 的元素
  • 使用新标题更新现有标题

我面临的问题:

  • 循环获取ID所在的索引
  • 更新现有标题,并将其替换为新标题

到目前为止我想到了什么:

    // 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

上面的代码只是部分代码,下面的任何内容都与问题无关。

最佳答案

直接方法...

所有这些都是循环遍历 BookList 并将用户输入的 bookIDbookID<本书的。如果找到匹配项,则 Book 引用将保留在变量 bookToEdit 中。

如果循环完成后,bookToEditnull,则 List 中没有匹配的 Book > 使用指定的 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");
}

s

如果你想做一些更奇特的事情,你可以使用...

// 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);

现在,您需要知道,这比之前的循环效率低,事实上它将循环遍历 newBook 的整个 List 并返回 ALL与 bookId 匹配的书籍(实际上应该只有一本)

map

最有效的方法是在 Map 中维护 Book,而不是在 List 中,以 为键书号。这样,当需要时,您可以简单地通过 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了解更多详情

关于java - 仅当 ArrayList 等于 bookID 时才更新它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com