對於我的課程,我們正在使用arraylist創建一個銀行帳戶。我試圖創建一個包含數組列表的構造函數,但是接收到無法解析的錯誤。任何幫助,將不勝感激,謝謝:)ArrayList無法解析
public class Bank {
public Bank() {
ArrayList<Account> bankArray = new ArrayList<Account>();
}
public void addNewAccount(Account a) {
bankArray.add(a); //error here
return;
}
public Account getAccountByIndex(int idx) {
return bankArray(idx); //error here
}
public ArrayList getAccounts() {
return bankArray; //error here
}
的錯誤我收到是
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
bankArray cannot be resolved
at Bank.addNewAccount(Bank.java:17)
bankArray是你的構造函數的局部變量。所以它在構造函數之外是不可見的。你需要把它變成一個領域。 –