2014-03-06 84 views
0

我無法理解和獲取我的程序的此功能。我正在創建一個庫,首先將多個對象輸入到數組列表中。這些對象由媒體的標題和格式組成。我需要能夠搜索數組列表中的對象來查找特定對象並將該對象標記爲「已簽出」。我一直在研究迭代器,並試圖理解如何讓它們在對象中找到指定的標題,但我遇到了麻煩。當我嘗試println(it.next())時,我得到MediaItem @ 3c4568f8;所以我知道返回正確格式的信息存在問題。任何關於如何搜索我的項目數組列表中的對象的幫助將不勝感激。如何迭代對象的數組列表以查找指定對象

import java.util.Iterator; 
import java.util.Scanner; 
import java.util.ArrayList; 

public class Library { 

    static ArrayList<MediaItem> items = new ArrayList<MediaItem>(); 
    static int menuOption; 
    static Scanner scan = new Scanner(System.in); 

    public static void main(String args[]) { 
     String title, format, loanedTo, dateLoaned; 
     boolean right = false; 

     do { 
      displayMenu(); 
      if (menuOption == 1) { 
       System.out.println("Enter Title: "); 
       title = scan.next(); 
       System.out.println("Enter format: "); 
       format = scan.next(); 
       addNewItem(title, format); 
      } else if (menuOption == 2) { 
       System.out.println("Enter the item title"); 
       title = scan.next(); 
       System.out.println("Who are you loaning it to?"); 
       loanedTo = scan.next(); 
       System.out.println("When did you loan it to them?"); 
       dateLoaned = scan.next(); 
       markItemOnLoan(title, loanedTo, dateLoaned); 
      } else if (menuOption == 3) { 
       for (MediaItem mi : items) { 
        System.out.println(mi.getTitle() + ", " + mi.getFormat()); 
       } 
      } else if (menuOption == 4) { 

      } else { 
       System.exit(1); 
      } 

     } while (!right); 
    } 

    static int displayMenu() { 
     boolean right = false; 

     do { 

      System.out.println("Menu: "); 
      System.out.println("1. Add New Item"); 
      System.out.println("2. Mark an item as on loan"); 
      System.out.println("3. List all items"); 
      System.out.println("4. Mark an item as returned"); 
      System.out.println("5. Quit"); 
      menuOption = scan.nextInt(); 

      if (menuOption < 1 || menuOption > 5) { 
       System.out.println("Invalid Number!"); 
      } 

      return menuOption; 
     } while (!right); 
    } 

    static void addNewItem(String title, String format) { 
     MediaItem b = new MediaItem(); 
     b.setTitle(title); 
     b.setFormat(format); 
     items.add(b); 

    } 

    static void markItemOnLoan(String title, String name, String date) { 
     Iterator<MediaItem> it = items.iterator(); 
     System.out.println(it.next()); 
    } 

} 





public class MediaItem { 

String title; 
    String format; 
    boolean onLoan; 
    String loanedTo; 
    String dateLoaned; 

    MediaItem() { 
     title = null; 
     format = null; 
     onLoan = false; 
     loanedTo = null; 
     dateLoaned = null; 

    } 

    MediaItem(String title, String format) { 
     title = new String(); 
     format = new String(); 

    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getFormat() { 
     return format; 
    } 

    public void setFormat(String format) { 
     this.format = format; 
    } 

    public boolean isOnLoan() { 
     return onLoan; 
    } 

    public void setOnLoan(boolean onLoan) { 
     this.onLoan = onLoan; 
    } 

    public String getLoanedTo() { 
     return loanedTo; 
    } 

    public void setLoanedTo(String loanedTo) { 
     this.loanedTo = loanedTo; 
    } 

    public String getDateLoaned() { 
     return dateLoaned; 
    } 

    public void setDateLoaned(String dateLoaned) { 
     this.dateLoaned = dateLoaned; 
    } 

    void markOnLoan(String name, String date) { 
     onLoan = true; 
    } 

    void markReturned() { 
     onLoan = false; 
    } 
} 
+0

您的對象沒有'toString()'方法,因此它將對象引用作爲字符串返回。換句話說,它調用'Object.toString()'而不是'MediaItem.toString()'(不存在)。 – turbo

+0

[打印引用對象]的可能的重複(http://stackoverflow.com/questions/8730127/printing-reference-to-an-object) – trutheality

+0

我同意的第一部分是重複的,但我也想知道如何在對象中搜索標題。 – YellowSoloCup

回答

5

[email protected]輸出結果是Object's toString() method

[T]他的方法返回一個字符串等於值:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

您還沒有覆蓋在MediatoString(),所以它繼承Object方法。

覆蓋toString(),返回要當你的對象傳遞給System.out.println並串轉換您Object電話toString()打印的字符串。

+0

謝謝!我現在能夠看到it.next()。現在我只需要弄清楚如何搜索特定的標題,時間來做更多的研究。 – YellowSoloCup

0

您應該實現一個toString()方法,該方法將返回類似於標題的內容而不是對象引用。另外,您可以實現一個比較器來準確確定對象是否匹配。