更新ArrayList,只有它等于bookID?

时间:2015-12-08 00:50:07

标签: java arraylist

我为我的库应用程序设置了一个类型为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

以上代码只是部分代码,低于此代码的任何内容都与问题无关。

2 个答案:

答案 0 :(得分:1)

直接方法......

所有这一切都会循环遍历List Book,并将用户输入的bookID与图书的bookID进行比较。如果找到匹配项,则Book引用将保留在变量bookToEdit中。

如果在循环完成后,bookToEditnull,那么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)

请使用地图解决您的问题。 您可以访问: http://www.tutorialspoint.com/java/java_map_interface.htm

以下是为您提供的一些示例代码:

async