簡體   English   中英

如何刪除存儲在數組列表中的值

[英]How to remove a value stored in an array list

所以我在這里有一些代碼,我想使用它擁有的 id 搜索這本書,然后從數量中刪除很多。

ArrayList = new ArrayList();

書 b1 = 新書(110, 5);

110 是 id,5 是數量,我如何在 ArrayList 中搜索 id,然后從數量中刪除 2 本書左右。

So I have some code here ....什么代碼? 如果您發布該代碼(並且盡可能不要發布它的圖像),那將是一個非常好的主意。 它有助於澄清許多不同的事情,並幫助這里的成員確定您可能遇到特定問題的地方。 我們在這里真正能做的就是猜測。 任何狀況之下.....

讓我們假設Book類是這樣的:

public class Book {
    
    int bookID;
    String bookTitle = "Unknown";
    String bookAuthor = "Unknown";
    int bookQuantity;

    
    // Constructor #1
    public Book() {  }
    
    // Constructor #2
    public Book(int bookID, int bookQuantity) {
        this.bookID = bookID;
        this.bookQuantity = bookQuantity;
    }
    
    // Constructor #3
    public Book(int bookID, String bookTitle, String bookAuthor, int bookQuantity) {
        this.bookID = bookID;
        this.bookTitle = bookTitle;
        this.bookAuthor = bookAuthor;
        this.bookQuantity = bookQuantity;
    }

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle) {
        this.bookTitle = bookTitle;
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public int getBookQuantity() {
        return bookQuantity;
    }

    public void setBookQuantity(int bookQuantity) {
        this.bookQuantity = bookQuantity;
    }

    @Override
    public String toString() {
        return new StringBuilder(String.valueOf(bookID)).append(", ").append(bookTitle).append(", ")
                .append(bookAuthor).append(", ").append(String.valueOf(bookQuantity)).toString();
    }
}

然后在另一個班級的某個地方,您的代碼可能是這樣的......

// An ArrayList to hold instances of Book
    java.util.ArrayList<Book> books = new java.util.ArrayList<>();

    // Add Books to ArryList
    books.add(new Book(110, 5));
    books.add(new Book(111, 2));
    books.add(new Book(112, 7));
    books.add(new Book(113, 3));
    
    // Display the current Books available
    System.out.println("Current Book Inventory:");
    System.out.println("=======================");
    for (Book b : books) {
        System.out.println(b.toString());
    }
    System.out.println();
    
    // REMOVAL
    int bookIDtoRemoveBooksFrom = 112; // Book ID to remove Books from
    int numberOfBooksToRemove = 3;     // The number of books to remove
    
    
    
    // Remove the desired quantity from desired Book instance.
    Book bookInstanceWhereBooksWereRemoved = null;
    for (Book b : books) {
        if (b.bookID == bookIDtoRemoveBooksFrom) {
            bookInstanceWhereBooksWereRemoved = b;
            int currentBookInventory = b.getBookQuantity();
            System.out.println("Current quantity of Book ID: " + bookIDtoRemoveBooksFrom 
                                + " is --> " + currentBookInventory);
            System.out.println();
            // Are there enough books of this ID to remove?
            if (currentBookInventory < numberOfBooksToRemove) {
                // No...
                System.out.println("There are only " + currentBookInventory + 
                        " books with the ID of " + bookIDtoRemoveBooksFrom + 
                        " in inventroy to remove the desired amount of (" 
                        + numberOfBooksToRemove + ") books!");
                System.out.println();
            }
            else {
                // Yes...
                b.setBookQuantity(currentBookInventory - numberOfBooksToRemove);
            }
            break; // Done - Break out of loop.
        }
    }
    
    // Display the current Books available AFTER the removal.
    System.out.println("NEW Book Inventory:");
    System.out.println("===================");
    for (Book b : books) {
        System.out.println(b.toString());
    }
    System.out.println();
    System.out.println("New quantity of Book ID: " 
                                + bookIDtoRemoveBooksFrom 
                                + " is --> " 
                                + bookInstanceWhereBooksWereRemoved.getBookQuantity());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM