簡體   English   中英

在 Java 中對數組列表的對象使用方法

[英]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)。 有幾種方法可以使它工作。

  • 在 BookCollection 中創建一個公共方法
  • 將字段更改為 public 然后直接訪問它books.bookList
  • 在 BookCollection 中為您的 bookList 創建一個 getter
  • 使 BookCollection 擴展 ArrayList

您在這里所做的是創建了一個裝飾器BookCollection ),它用附加功能包裝了一個ArrayList (在這種情況下,是一個基於文件預填充它的構造函數)。 這為您提供了如何使用List中的Book兩種選擇:

  1. 通過 getter 使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 提供的方法。

Java 方法添加 ArrayList 未定義類型<object><div id="text_translate"><p>我正在為我的作業構建一個 java 程序,我必須將產品添加到特定商店。 嘗試從 Store class 添加到 ArrayList 時遇到問題。</p><p> 我有 class 產品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存儲如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object>

[英]Java Method add ArrayList is undefined for the type <OBJECT>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 使用equals方法從arraylist獲取對象 - java 使用Java中的方法在HashMap中使用ArrayList Java 方法添加 ArrayList 未定義類型<object><div id="text_translate"><p>我正在為我的作業構建一個 java 程序,我必須將產品添加到特定商店。 嘗試從 Store class 添加到 ArrayList 時遇到問題。</p><p> 我有 class 產品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存儲如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> JAVA更新ArrayList對象方法未更新 傳遞一個ArrayList <Object> 作為方法的參數,處理arrayList並將其返回-Java 在arraylist對象中使用split()方法代碼是不完整的 使用ArrayList存儲對象並使用main方法進行測試 使用Java中的boolean方法將一個ArrayList與另一個ArrayList進行比較 Java:使用一種方法從ArrayList獲取值 使用Apache Digester時,“沒有這樣的可訪問方法:對象:java.util.ArrayList上的setFields()”
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM