Java 如何循环遍历account类

Java 如何循环遍历account类,java,for-loop,Java,For Loop,我正在尝试创建一个函数,该函数循环遍历帐户,并根据给定的帐号查找帐户。它将尝试使用.equals来查看值是否匹配 这就是我目前所拥有的和我正在尝试的逻辑 public Account getAccountByNumber(int accountNumber) { for (Account acc : account) { if (acc.getAccountNumber().equals(String.valueOf(accountNumber))

我正在尝试创建一个函数,该函数循环遍历帐户,并根据给定的帐号查找帐户。它将尝试使用.equals来查看值是否匹配

这就是我目前所拥有的和我正在尝试的逻辑

    public Account getAccountByNumber(int accountNumber) {
        for (Account acc : account) {
            if (acc.getAccountNumber().equals(String.valueOf(accountNumber)))
                return account;
        }
    }
从我对for循环语法的理解来看 我没有account类的数组

 (dataType var : array) {
}
Account是对公共帐户类的引用。

创建帐户类的数组列表->存储帐户对象->在其中循环

private static void main(String args[]){
    ArrayList<Account> accounts = new ArrayList<>();

    Account account1 = new Account(params1); // Constructor
    accounts.add(account1);

    Account account2 = new Account(param2); // Constructor
    accounts.add(account2);

    Account account3 = new Account(params3); // Constructor
    accounts.add(account3);

    Account account4 = new Account(params4); // Constructor
    accounts.add(account4);

}

public static Account getAccountByNumber(int accountNumber) {
    for (Account acc : accounst) {
        if (acc.getAccountNumber().equals(String.valueOf(accountNumber)))
            return account;
    }
}

account变量是什么?account类是什么样的?您循环的account变量是什么?如果account是array或ArrayList类似ArrayList account,您的循环是正确的,并且可以工作。我正在尝试循环account类,并根据给定的account编号查找account。我没有account的arrylist班我需要创建一个吗?