在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。我只是对逻辑有点困惑,我以前从来没有真正使用过数组列表

你可以给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<...)计数循环。

,在主方法中,只需调用新方法:

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 at索引中的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!");
        }
    }
}

首先(如果你不想使用流等)你需要从数组列表中获取对象。您可以使用ArrayList.get(int index),然后调用该方法,或者在每个循环中使用:

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

问题是你不能从main访问私有字段(bookList)。有几种方法可以使它工作。

  • 在BookCollection
  • 中创建一个公共方法
  • 将字段更改为public,然后直接访问books.bookList
  • 在BookCollection
  • 中为bookList添加一个getter
  • 创建BookCollection扩展ArrayList

您在这里所做的是创建一个装饰器 (BookCollection),它用附加功能包装ArrayList(在本例中,是一个基于文件预填充它的构造函数)。这为您提供了如何在List中使用Book的两个选项:

  1. List可以通过getter访问,然后计算出来。使语句变长,但对于少量使用是可以接受的:

    if(books.getBooks().get(index).isLong()) {
        //The book at index is long
    }
    
  2. 更常见的是,您将在装饰器中创建提供通用功能的方法,如:

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

    那么您只需从业务逻辑中调用decorator方法。你甚至可以制作更复杂的方法,比如GhostCat提供的方法。

最新更新