I have created a method that should ideally take a single Account object and then add it an array of accounts, but the problem is that the entered Account “add” overrides every existing Account in the array and sets them all equal to add and I am not sure why. Additionally, prior to doing anything the array accounts gets set to the entered account “add” and I am completely puzzled as to why this is. Sorry if I am missing something blatantly obvious, but any help would be appreciated.
public void addAccount(Account add)
{
if (count < accounts.length)
{
accounts[count] = add;
count++;
System.out.println("Added " + add.toString() + " to list of accounts");
}
else
{
accounts = expand(10);
addAccount(add);
}
}
Your
addAccount()method looks ok, your problem is most likely in your call to it. Based on how I’ve seen people get the same problem earlier, the reason you’re seeing it is most likely that you’re doing something like;…which would create one account, change it, add it, change it, add it etc. What you need to remember is that you’re creating a single account, so changing it after adding it will change the values you earlier added to the array too. What you’d need to do is;
which will create 10 accounts and would work better.