2016-12-05 75 views
-2

我將代碼配對到我遇到問題的方法。這似乎是工作,直到我再次加載文件,它沒有任何內容。 (我還沒有完全理解如何在執行第二次加載之前清除ArrayList,但這是以後的)。java howto加載並保存ArrayList對象

對不起,如果這是隱藏在某個其他術語下的某處我還沒有學到,但這是一個明天到期的項目,我在我的智慧結束。

import java.util.*; 
import java.io.*; 

public class MainATM3 { 

    public static ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(); 
    public static ClientAccount editBankAccount = new ClientAccount("placeholder",1234,1);; 

    public static void main(String[] args) { 

// Create ATM account ArrayList 
    ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(); 
// Get Account data from files 
initialLoadATMAccounts(accounts); 
System.out.println("Loaded "+accounts.size()); 

System.out.println("before Array "+(accounts.size())); 
    accounts.add(0,new ClientAccount("Jess",500,1830)); 
    accounts.add(1,new ClientAccount("Mary",1111.11,7890)); 
System.out.println("after Array "+(accounts.size())); 

saveATMAccounts(accounts); 
System.out.println("saved "+(accounts.size())); 

initialLoadATMAccounts(accounts); 
System.out.println("Loaded "+accounts.size()); 

    System.out.println("Logged Out"); 

    }  

// Save ArrayList of ATM Objects //call by: saveATMAccounts(accounts); 
    public static void saveATMAccounts(ArrayList<ClientAccount> saveAccounts) { 
    FileOutputStream fout = null; 
    ObjectOutputStream oos = null; 
    try{ 
     fout=new FileOutputStream("ATMAccounts.sav"); 
     oos = new ObjectOutputStream(fout); 
     oos.writeObject(accounts); 
     System.out.println("objects written "+(accounts.size())); 
     } catch (Exception ex) { 
     ex.printStackTrace(); 
     } finally { 
     if (fout != null) { 
      try { 
      fout.close(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
      } 
     if (oos != null) { 
      try { 
      oos.close(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
     } 
    } 
    } 


    // INITIAL Load ArrayList of ATM Objects //call by: initialLoadATMAccounts(accounts); 
    public static void initialLoadATMAccounts(ArrayList<ClientAccount> loadAccounts){ 
    FileInputStream fIS = null; 
    ObjectInputStream oIS = null; 
    try{ 
     fIS=new FileInputStream("ATMAccounts.sav"); 
     oIS = new ObjectInputStream(fIS); 
     ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>) oIS.readObject(); 
     oIS.close(); 
     fIS.close(); 
    } 
    catch(Exception exc){ 
     exc.printStackTrace(); 
    } 
    } 
} 


import java.io.Serializable; 
public class ClientAccount implements Serializable { 

    public String accountName; 
    public double accountBalance; 
    public int accountPIN; 

    public ClientAccount(String accountName, double accountBalance, int accountPIN){ 
     this.accountName=accountName; 
     this.accountBalance=accountBalance; 
     this.accountPIN=accountPIN; 
    } 

// Account Name Methods  
    public String getAccountName() { 
     return accountName; 
    } 
    public void setAccountName(String name) { 
     accountName = name; 
    } 

// Account Balance Methods 
    public double getAccountBalance() { 
     return accountBalance; 
    } 
    public void setAccountBalance(double balance) { 
     accountBalance = balance; 
    } 

// PIN Methods 
    public int getAccountPIN() { 
     return accountPIN; 
    } 
    public void setAccountPIN(int newPIN) { 
     accountPIN = newPIN; 
    } 

} 
+0

我只是初始加載。你從哪裏重新加載? – MordechayS

+0

你想在'initialLoadATMAccounts(ArrayList loadAccounts)'中做什麼?你爲什麼不在你的方法的任何地方使用'loadAccounts'參數?如果你不使用它,那麼爲什麼你的方法有這個參數?有些東西告訴我,你可能需要閱讀[Java是「通過引用傳遞」還是「傳遞值」?](http://stackoverflow.com/questions/40480/is-java-pass-by-基準或通按值)。 – Pshemo

+0

您應該使用[try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。另外,你可能希望你的加載方法返回一個'ArrayList'。 – 4castle

回答

0

不是傳遞所需的陣列initialLoadATMAccounts爲PARAM你應該返回新的,裝陣:

public static List<ClientAccount> initialLoadATMAccounts(){ 
    FileInputStream fIS = null; 
    ObjectInputStream oIS = null; 
    try{ 
     fIS=new FileInputStream("ATMAccounts.sav"); 
     oIS = new ObjectInputStream(fIS); 
     ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>)  oIS.readObject(); 
     oIS.close(); 
     fIS.close(); 
     return loadAccounts; 
    } 
    catch(Exception exc){ 
     exc.printStackTrace(); 
    } 
} 

BTW:像Eclipse的一個IDE會發出警告,在那裏你覆蓋帕拉姆loadAccounts 。