1,105,416 Community Members

how to Delete an Array?

Member Avatar
Taimoor Rana
Light Poster
47 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

// 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

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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)

Member Avatar
blue6dImension
Newbie Poster
2 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 4 Years Ago by BestJewSinceJC and blue6dImension
Member Avatar
Taimoor Rana
Light Poster
47 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you to the both of you.

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article