gpt4 book ai didi

Java在列表末尾添加

转载 作者:行者123 更新时间:2023-11-29 05:57:02 27 4
gpt4 key购买 nike

我有一个读书类。我想在列表末尾添加一个元素

public class Book {
private String title;
private String authorName;
private double price;


/**
* @param title
* @param authorName
* @param price
*/
public Book(String title, String authorName, double price) {
setTitle(title);
setAuthorName(authorName);
setPrice(price);
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthorName() {
return authorName;
}

public void setAuthorName(String authorName) {
this.authorName = authorName;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = Math.abs(price);
}

还有一个 Booklist 类,它有一个方法 append 可以将书籍添加到列表中,但我无法弄清楚如何将值输入到列表中

public class BookList {

/**
* books will be stored in an array of strings
*/

private Book[] books;
// *** TODO *** you will need to add more private members variables

public BookList(int N) {
// TODO Auto-generated method stub
}



public void append(Book book) throws BookListFull {
// TODO Auto-generated method stub
}

我想在列表末尾添加元素请帮助我如何做谢谢

最佳答案

public class BookList {
List<Book> bookList= new ArrayList<Book>();

...
...
public void append(Book book) throws BookListFull {
bookList.add(book);
}
}

关于Java在列表末尾添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11686029/

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