无法在不同的 class 中填充 ArrayList<>
Can't populate the ArrayList<> in a different class
我必须在 class Librarian
中填充我的 ArrayList<FictionBook>
,但是当我使用 fictionBooks.add();
时,它无法识别变量 fictionBooks
。这是我当前的代码。另外我不确定如何用 add(String, String)
.
填充它
public class FictionBook {
private String author;
private String title;
private static final int AVAILABLE = 1;
private static final int ONLOAN = 0;
public FictionBook(String theAuthor, String theTitle){
this.author = theAuthor;
this.title = theTitle;
}
public int borrow() {
System.out.println("The book is already borrowed.");
return ONLOAN;
}
public int returnB() {
System.out.println("The book was just returned.");
return AVAILABLE;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
我在这里创建数组列表:
import java.util.*;
public class Library {
private static final int CAPACITY = 200;
public ArrayList<FictionBook> fictionBooks = new ArrayList<FictionBook>();
public Library() {
//fictionBooks = new ArrayList<FictionBook>();
}
public void addBook(FictionBook book) {
fictionBooks.add(book);
}
public void deleteBook(FictionBook book) {
fictionBooks.remove(book);
}
public void borrowBook(FictionBook book) {
}
public void returnBook(FictionBook book) {
}
public void sort() {
Collections.sort(fictionBooks, String.CASE_INSENSITIVE_ORDER);
}
}
我需要在此处填充它包含 ("Author", "Title"):
public class Librarian {
public static void main(String[] args) {
fictionBooks.add("Jon", "Tiltle");
}
}
fictionBooks
属于Library
class。您必须先实例化它。
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.addBook(new FictionBook("Jon", "Tiltle"));
}
}
您需要一个 Library
的实例:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.fictionBooks.add(new FictionBook("Jon", "Tiltle"));
}
}
或者使用 Library
的方法更好:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.addBook(new FictionBook("Jon", "Tiltle"));
}
}
您需要在 Library 中公开一个方法 getBooks,然后 Librarian 会像下面这样使用 Library:
public static void main(String[] args){
Library library = ....;
library.getFictionBooks().add(new FictionBook("Jon", "Tiltle"));
}
注意,您可能应该考虑图书馆与图书管理员之间的关系,并说图书馆有图书管理员,您可以在图书馆内组成图书管理员。
您可以在图书管理员中使用此代码添加一本书 类 主要方法:
Library library = new Library();
FictionBook yourBookToAdd = new FictionBook("Jon", "Tiltle")
library.addBook(yourBookToAdd );
希望对您有所帮助!
您的 fictionBooks
ArrayList 属于 您的 Library
class。这意味着您首先需要创建一个 Library
:
的实例
Library library = new Library();
然后,由于您的 fictionBooks
变量是 FictionBook
的列表,您必须先创建一个,然后才能将其添加到列表中,如下所示:
FictionBook fictionBook = new FictionBook("Jon", "Title");
现在您可以访问新库的 fictionBooks
变量并将新创建的 fictionBook
添加到其中:
library.fictionBooks.add(fictionBook);
但是由于您已经在 Library
中获得了一个名为 addBook(FictionBook)
的方法,我建议您改用它,如下所示:
library.addBook(fictionBook);
最后两步可以压缩成一行,所以完成的代码如下所示:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
//Creating and adding the Fiction Book to the list in a single line of code.
library.addBook(new FictionBook("Jon", "Title"));
}
}
这里已经有很多正确答案,但我只是想我会进一步详细说明,以防它可以帮助您更好地理解问题。 :)
我必须在 class Librarian
中填充我的 ArrayList<FictionBook>
,但是当我使用 fictionBooks.add();
时,它无法识别变量 fictionBooks
。这是我当前的代码。另外我不确定如何用 add(String, String)
.
public class FictionBook {
private String author;
private String title;
private static final int AVAILABLE = 1;
private static final int ONLOAN = 0;
public FictionBook(String theAuthor, String theTitle){
this.author = theAuthor;
this.title = theTitle;
}
public int borrow() {
System.out.println("The book is already borrowed.");
return ONLOAN;
}
public int returnB() {
System.out.println("The book was just returned.");
return AVAILABLE;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
我在这里创建数组列表:
import java.util.*;
public class Library {
private static final int CAPACITY = 200;
public ArrayList<FictionBook> fictionBooks = new ArrayList<FictionBook>();
public Library() {
//fictionBooks = new ArrayList<FictionBook>();
}
public void addBook(FictionBook book) {
fictionBooks.add(book);
}
public void deleteBook(FictionBook book) {
fictionBooks.remove(book);
}
public void borrowBook(FictionBook book) {
}
public void returnBook(FictionBook book) {
}
public void sort() {
Collections.sort(fictionBooks, String.CASE_INSENSITIVE_ORDER);
}
}
我需要在此处填充它包含 ("Author", "Title"):
public class Librarian {
public static void main(String[] args) {
fictionBooks.add("Jon", "Tiltle");
}
}
fictionBooks
属于Library
class。您必须先实例化它。
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.addBook(new FictionBook("Jon", "Tiltle"));
}
}
您需要一个 Library
的实例:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.fictionBooks.add(new FictionBook("Jon", "Tiltle"));
}
}
或者使用 Library
的方法更好:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
library.addBook(new FictionBook("Jon", "Tiltle"));
}
}
您需要在 Library 中公开一个方法 getBooks,然后 Librarian 会像下面这样使用 Library:
public static void main(String[] args){
Library library = ....;
library.getFictionBooks().add(new FictionBook("Jon", "Tiltle"));
}
注意,您可能应该考虑图书馆与图书管理员之间的关系,并说图书馆有图书管理员,您可以在图书馆内组成图书管理员。
您可以在图书管理员中使用此代码添加一本书 类 主要方法:
Library library = new Library();
FictionBook yourBookToAdd = new FictionBook("Jon", "Tiltle")
library.addBook(yourBookToAdd );
希望对您有所帮助!
您的 fictionBooks
ArrayList 属于 您的 Library
class。这意味着您首先需要创建一个 Library
:
Library library = new Library();
然后,由于您的 fictionBooks
变量是 FictionBook
的列表,您必须先创建一个,然后才能将其添加到列表中,如下所示:
FictionBook fictionBook = new FictionBook("Jon", "Title");
现在您可以访问新库的 fictionBooks
变量并将新创建的 fictionBook
添加到其中:
library.fictionBooks.add(fictionBook);
但是由于您已经在 Library
中获得了一个名为 addBook(FictionBook)
的方法,我建议您改用它,如下所示:
library.addBook(fictionBook);
最后两步可以压缩成一行,所以完成的代码如下所示:
public class Librarian {
public static void main(String[] args){
Library library = new Library();
//Creating and adding the Fiction Book to the list in a single line of code.
library.addBook(new FictionBook("Jon", "Title"));
}
}
这里已经有很多正确答案,但我只是想我会进一步详细说明,以防它可以帮助您更好地理解问题。 :)