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列表對象使用方法
thelist.get(theindex).isLong() –