Hello, I am experiencing problems when trying to remove elements from an array.
I've been asked to create a little program which creates bank accounts containing (name, address, bank account number, balance etc.). I have successfully implemented all the functions except deleteAccount (I would also like to add that my array has a fixed size of 10 elemets).
Here's my method:
public void deleteAccount(String accountNumber)
{
for (int acc = 0; acc < nextAccountIndex; acc++)
{
if (accounts[acc].getAccountNumber().equals(accountNumber))
{
accounts[acc] = null;
for (int remove = acc; remove < nextAccountIndex; remove++)
{
accounts[remove] = accounts[remove+1];
}
}
accounts[nextAccountIndex-1] = null;
nextAccountIndex--;
}
}
Whenever I create a new account, the accounts are created as it follows: 1001, 1002, 1003 etc., but when I try to delete account no: 1002 or 1003 I am left with just the first one. My question is: If I have filled the array with 10(or just 5) elements {1, 2, 3, 4, 5, etc.}, How can all the values move down, if I delete the value of the second or any other element in the array.
Thanks in advance!