2016-09-13 32 views
-1
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); 


} 
} 

這裏是所有相關的代碼。我的項目是閱讀一個文本文件,其中包含有關這些書籍的信息,並將它們放入書籍對象的數組列表中。我的問題是:我將如何去關於在ArrayList中的類Book中發現的isLong()方法?該方法的要點是,如果一個對象具有> 500個頁面,則返回true。如果不是,它將返回false。我只是對邏輯感到困惑,之前我從來沒有真正與Arraylists合作過。對Java中的Array列表對象使用方法

+0

thelist.get(theindex).isLong() –

回答

1

您可以添加另一種方法來BookCollection:

public void printLongBooks() { 
    for (Book book : bookList) { 
    if (book.isLong()) { 
     well, book is long ... 
    } else { 
     obviously, it is short ... 
} 

以上使用所謂的「爲每個」 Java中循環的風格,你可以用它來循環每一個數組/集合;而不是「老派」for (int i=0; i<...)計數循環。

和你的主要方法中,只需調用新方法:

books.printLongBooks() 

和一些通用提示:

A)isLong()可以減少到一個班輪:return length > 500;

B)閱讀從文件中的東西不是你直接在構造函數中做的。相反,您應該爲此創建一個專用方法,然後您可以在構造函數中調用

0

而不是將邏輯創建書目列表放入構造函數中,您應該創建一個名爲getBookCollection()的方法,該方法返回一個ArrayList 。

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 
     } 
    } 
0

可以使用for遍歷ArrayList的元素進行迭代,並調用它的對象的每一個方法。

for(Book book : bookList){ 
    boolean isLong = book.isLong(); 
} 
0

你有一個List<Book>,凡在List每個索引包含Book。使用List.get(index)方法獲取索引處給定的List中的Object。例如:bookList.get(0)在索引0處獲得Book。一旦您擁有了Object,就可以正常使用它。

我假設你有一些方法可以獲得BookCollection中的bookList,並且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!"); 
     } 
    } 
} 
0

首先(如果你不想使用流等),你需要從ArrayList中獲取對象。您可以通過使用ArrayList.get(int index),然後調用該方法,或通過使用每個循環做到這一點:

for(Book book : bookList) { 
    System.out.println(book.isLong()); 
} 

的問題是,你不能從主訪問私有字段(書目)。有幾種方法可以使其工作。

  • 內BookCollection創建一個公共方法
  • 變化領域的公共,然後直接訪問它books.bookList
  • 做一個getter你的書目中BookCollection
  • 讓BookCollection延伸的ArrayList
0

什麼你在這裏完成的是創建一個修飾器BookCollection),它包裹了一個ArrayList具有附加功能(在這種情況下,它是一個基於文件預填充的構造函數)。這爲您提供瞭如何利用一個BookList兩個選項:

  1. 通過吸氣充分利用List訪問,然後解決它。使得報表長時,又是少量使用的接受:

    if(books.getBooks().get(index).isLong()) { 
        //The book at index is long 
    } 
    
  2. 更常見的是,你會做的裝飾方法,它提供通用的功能,例如:

    public boolean isLong(int index) { 
        return bookList.get(index).isLong(); 
    } 
    

    然後你只需從業務邏輯中調用裝飾器方法。您甚至可以製作更復雜的方法,例如GhostCat提供的方法。