【发布时间】:2017-01-21 11:36:42
【问题描述】:
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。我只是对逻辑有点困惑,而且我以前从未真正使用过 Arraylist。
【问题讨论】:
-
thelist.get(theindex).isLong()
标签: java class arraylist methods