java - 带数组的迭代器循环

标签 java foreach

我确信这里有一个非常简单快捷的问题...所以假设我有一个 Account 类,如下所示:

import java.text.NumberFormat;

public class Account
{
    private final double RATE = 0.03; // interest rate of 3.5%
    private long acctNumber;
    private double balance;
    private String name;

    //-----------------------------------------------------------------
    // Sets up the account by defining its owner, account number,
    // and initial balance.
    //-----------------------------------------------------------------
    public Account (String owner, long account, double initial)
    {
        name = owner;
        acctNumber = account;
        balance = initial;
    }
    //----------------------------------------------------------------- 
    // Deposits the specified amount into the account. Returns the
    // new balance.
    //-----------------------------------------------------------------
    public double deposit (double amount)
    {
        balance = balance + amount;
        return balance;
    }
    //-----------------------------------------------------------------
    // Withdraws the specified amount from the account and applies
    // the fee. Returns the new balance.
    //-----------------------------------------------------------------
    public double withdraw (double amount, double fee)
    {
        balance = balance - amount - fee;
        return balance;
    }
    //-----------------------------------------------------------------
    // Adds interest to the account and returns the new balance.
    //-----------------------------------------------------------------
    public double addInterest ()
    {
        balance += (balance * RATE);
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns the current balance of the account.
    //-----------------------------------------------------------------
    public double getBalance ()
    {
        return balance;
    }
    //-----------------------------------------------------------------
    // Returns a one-line description of the account as a string.
    //-----------------------------------------------------------------
    public String toString ()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        return acctNumber + "\t" + name + "\t" + fmt.format(balance);
    }
}

我创建了此处显示的 Bank 类...

public class Bank 
{   
    Account[] accounts;// = new Account[30];
    int count=0;
    String name;

    public Bank(String name)
    {
        this.name = name; 
        accounts = new Account[30];
    }
    public void addAccount(Account acct)
    {
        accounts[count] = acct;
        count++;
    }
    public void addInterest()
    {
        //for (Account acct : accounts)
            //acct.addInterest();
        for(int i = 0; i < count; i++)
            accounts[i].addInterest();
    }
}

如果我尝试将 addInterest() 方法与 for (Account acct:accounts) 循环您会看到注释掉了。有人可以告诉我这是为什么吗?我认为这些循环是等效的。提前致谢。

最佳答案

可迭代数组上的 for 循环会迭代所有 30 个元素,而不仅仅是您真正添加的元素。

您可以使用ArrayList<Account>并根据需要添加元素。这允许您省略计数字段:

public class Bank 
{   
    ArrayList<Account> accounts = new ArrayList<Account>();
    String name;

    public Bank(String name)
    {
        this.name = name; 
    }
    public void addAccount(Account acct)
    {
        accounts.add(acct);
    }
    public void addInterest()
    {
        for (Account acct : accounts)
            acct.addInterest();
    }
}

关于java - 带数组的迭代器循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605091/

相关文章:

jquery - JQuery 中的迭代

java - 逐位比较两个 4 位整数

java - 无法通过拖放批处理文件来访问 jar 文件

java - 如何在 Spring session 中存储每个用户的数据?

java - 将 ASN1ObjectIdentifier 转换为 Java 中的可读字符串

php - Foreach 用于数组内部的数组

php - 在 foreach 循环中正确使用 foreach 循环

java - 使用正则表达式在字符串之间添加空格

c# - foreach 会自动调用 Dispose 吗?

php - 使这个 Magento 嵌套循环更好