2015-10-30 52 views
-1

所以問題是方法「print()」只能在主void中使用。當我嘗試在「changeAccount()」中使用它時,它說「找不到符號」。僅在主void中查找方法(無法找到符號,符號:方法tulosta(),location:class Object)

public class Main { 

    public static ArrayList createAccount(ArrayList accountList) { 

     Account account1 = new Account(); 
     accountList.add(account1); 
     return accountList; 
    } 

    public static int changeAccount(ArrayList accountList) { 

     accountList.get(0).print(); 
    } 

    public static void main(String[] args) { 

     ArrayList<Account> accountList = new ArrayList<>(0); 
     createAccount(tiliTaulukko); 
     accountList.get(0).print(); 
    } 
} 

現在,這裏是從哪裏調用print methos。

public class Account { 

    public void print(){ 

    } 

} 
+0

你上哪兒去定義打印?請創建[MCVE](http://stackoverflow.com/help/mcve) – StackFlowed

+2

您可以編輯您的問題以包含完整的錯誤信息嗎? – azurefrog

+0

參數tiliTaulukko代表什麼?我沒有發現它在任何地方初始化。 –

回答

3

changeAccount方法,參數accountList被聲明爲ArrayList,不ArrayList<Account>,所以accountList.get(0)的類型將是java.lang.Object,其不具有限定的print()方法。

+0

明白了,謝謝! – iWillBeMaster

2

你的問題是從accountList.get(0)返回的類型在你的兩種方法中是不一樣的。

在你main方法,您已經定義accountListArrayList<Account>

public static void main(String[] args) { 
    ArrayList<Account> accountList = new ArrayList<>(0); 
    ... 
} 

所以,當你調用accountList.get(0),你會得到一個Account回來了,可沒有一個錯誤在其上運行print()

在你changeAccount方法,您已經定義了accountList參數作爲原料的ArrayList:

public static int changeAccount(ArrayList accountList) { 
    ... 
} 

所以,當你調用accountList.get(0),你會得到一個Object回,它沒有print()方法。

更改您的參數類型爲ArrayList<Account>

public static int changeAccount(ArrayList<Account> accountList) { 
    //This should now work 
    accountList.get(0).print(); 
    ... 
} 
相關問題