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!

Lines 17,18 are executed regardless of whether you have found the account to delete or not - they are outside the if test.

Lines 17,18 are executed regardless of whether you have found the account to delete or not - they are outside the if test.

Thank you very much for the quick reply! I've been trying to fix this 'error' for about an hour now. :D

OK - if you're happy then mark this "solved" so people know whether or not to read it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.