[英]Using a method on an object of an arraylist in Java
public class Book
{
private String isbn, author, area;
private int length;
public Book(String isbn, String author, String area, int length)
{
this.isbn=isbn;
this.author=author;
this.area=area;
this.length=length;
}
public boolean isLong()
{
if (length>500)
return true;
else
return false;
}
}
public class BookCollection
{
private ArrayList<Book> bookList = new ArrayList<Book>();
public BookCollection()throws IOException
{
Scanner fileScan;
String oneLine;
String isbn, author, area;
int length;
fileScan = new Scanner (new File("books.txt"));
while (fileScan.hasNext())
{
isbn=fileScan.next();
author=fileScan.next();
area=fileScan.next();
length=fileScan.nextInt();
bookList.add(new Book(isbn, author, area, length));
}
}
public class TestBookCollection
{
public static void main (String[] args)throws IOException
{
BookCollection books = new BookCollection();
System.out.println(books);
}
}
好的,這是相關的代碼。 我的項目是讀取一個包含這些書籍信息的文本文件,並將它們放入書籍對象的數組列表中。 我的問題是:我將如何在數組列表中的對象上調用類 Book 中的 isLong() 方法? 該方法的要點是,如果一個對象有 >500 頁,則它返回 true。 如果不是,它將返回false。 我只是對邏輯有點困惑,而且我以前從未真正使用過 Arraylists。
您可以向 BookCollection 添加另一個方法:
public void printLongBooks() {
for (Book book : bookList) {
if (book.isLong()) {
well, book is long ...
} else {
obviously, it is short ...
}
上面使用了 Java 中所謂的“for each”循環樣式,您可以使用它來循環每個數組/集合; 而不是for (int i=0; i<...)
計數循環的“老派”。
在您的 main 方法中,您只需調用新方法:
books.printLongBooks()
以及一些通用提示:
A) isLong() 可以簡化為單行: return length > 500;
B)從文件中讀取內容不是您在構造函數中直接執行的操作。 相反,您應該為此創建一個專用方法,然后您可以在構造函數中調用該方法
與其將創建書籍列表的邏輯放在構造函數中,不如創建一個名為 getBookCollection() 的方法,該方法返回一個數組列表。
public List<Book> BookCollection()throws IOException
{
List<Book> bookList = new ArrayList<Book>();
//logic to create booklist
return booklist;
}
獲得書籍列表后,您可以運行增強的 for 循環並檢查每本書中的頁面。
for(Book book: bookList){
if(book.isLong()){
//logic
} else {
//logic
}
}
您可以使用for
循環遍歷ArrayList
的元素並對其每個對象調用該方法。
for(Book book : bookList){
boolean isLong = book.isLong();
}
您有一個List<Book>
,其中List
中的每個索引都包含一個Book
。 使用List.get(index)
方法獲取給定List
中索引處的Object
。 例如: bookList.get(0)
在索引 0 處獲取Book
。一旦你有了那個Object
,你就可以正常使用它了。
我假設您有某種方法可以獲取bookList
BookCollection
以及Book
的名稱?
public static void main(String[] args){
BookCollection books = new BookCollection(); // Make a new BookCollection
List<Book> booksInCollection = books.getBooksList(); // Get the Books inside BookCollection
// Go through each book in booksInCollection
for(int index = 0; index < booksInCollection.size(); index++){
// Get the Book Object at index
Book currentBook = booksInCollection.get(index);
if(currentBook.isLong()){
System.out.println(currentBook.getName() + " is long!");
}
}
}
首先(如果你不想使用流等)你需要從 ArrayList 獲取對象。 您可以通過使用ArrayList.get(int index)
然后調用該方法或使用 for 每個循環來做到這一點:
for(Book book : bookList) {
System.out.println(book.isLong());
}
問題是您無法從 main 訪問私有字段(bookList)。 有幾種方法可以使它工作。
books.bookList
您在這里所做的是創建了一個裝飾器( BookCollection
),它用附加功能包裝了一個ArrayList
(在這種情況下,是一個基於文件預填充它的構造函數)。 這為您提供了如何使用List
中的Book
兩種選擇:
通過 getter 使List
可訪問,然后解決它。 使語句很長,但這對於少量使用是可以接受的:
if(books.getBooks().get(index).isLong()) { //The book at index is long }
更常見的是,您將在裝飾器中創建提供通用功能的方法,如下所示:
public boolean isLong(int index) { return bookList.get(index).isLong(); }
然后您只需從您的業務邏輯中調用裝飾器方法。 您甚至可以制作更復雜的方法,例如 GhostCat 提供的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.