// close an account with a given number
public void closeAccount(int id) {
}

this is a bank project... a customer account is created(array). i would like to know how to delete an array? let say: account number 185649 is found in array[15]. then i would like to delete this array decal the rest to the left.

array[15] is deleted.
so array[16] become array[15].... array [17] become array[16].

thanks in advance

Recommended Answers

All 4 Replies

It is called deleting an element from an array, not deleting the array. Just a little terminology thing - you sounded like you were saying you wanted to delete the entire array. Anyway, you can do this by moving each element back one like you suggested. So you'd need to use a for loop to do that. And you also need to keep track of what index the last element of the array is stored at (since your array probably won't be full, and definitely won't be full after you delete an element)

using a loop with index tracking is a good idea, i would approach it differently thought

i would go:
1: delete element of that array[x]
2: create a new array with same size or newArray = new long [array.length*2]
3: copy array up til an empty element, stop, then arrayCopy(parameter) again from the next element with value
4. you can even set a condition statement to determine when to stop at what index when copying, but doing it this way, if you had to delete more than 1 customer, or more than 1 element, you wouldn't have to loop so many time

thank you to the both of you.

1: delete element of that array[x]
2: create a new array with same size or newArray = new long [array.length*2]
3: copy array up til an empty element, stop, then arrayCopy(parameter) again from the next element with value
4. you can even set a condition statement to determine when to stop at what index when copying, but doing it this way, if you had to delete more than 1 customer, or more than 1 element, you wouldn't have to loop so many time

Your approach is wasteful and just bad programming. It wastes memory by creating another array, which is unnecessary. And it takes longer than my method takes because you have to copy all of the array's elements (other than the one that was deleted). My approach only copies the elements after the empty index. This means that your algorithm is always O(n) whereas mine is faster the closer the removed element is to the end of the array. Furthermore, you should only increase the size of the array in certain circumstances, such as when the number of elements in your array has surpassed 80% of the array's capacity. I've never heard of increasing the capacity of an array when an element is removed.

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.