2014-04-13 44 views
0

我無法從我的客戶類中創建的Account ArrayList中打印結果。顯示子類中的Arraylist的內容

public static void customerInteraction(Customer cust) throws Exception { 
    Scanner read = new Scanner(System.in); 



    int custInput; 

    //write the logic here to interact with the customer and the account(s) 
    System.out.println("Your customer was successfully located."); 
    System.out.println("Hi "+cust.fullName()); 
    System.out.println("Here are your Accounts."); 

    // display the Accounts in the list 
    **for (int i = 0; i < cust.acctList.size(); i++) { 
     System.out.printf(" %s", cust.acctList.get(i));** 
    **}** 

我可以創建一個客戶和賬戶信息保存到acctlist帳戶的ArrayList

Customer Class 
public void addAccount(String acctType) { 
     if (acctType.equalsIgnoreCase("CHECKING")) {acctList.add(new Checking(this.custId)); 
     } else { 
      acctList.add(new Savings(this.custId)); 
     } 
} 
+0

你得到了什麼錯誤? – Mureinik

+0

'acctList'是公開的嗎? –

+0

ArrayList acctList = new ArrayList <>();在客戶類別中聲明 – SvenJG

回答

0

使用名字我忘了從Java 1.6+部分:

它更可讀,應該幫助你找出是否還有其他東西G。

for (E element : cust.acctList) { 
    // Do stuff with each element E 
enter code here}