![]() |
Remove an element from an array ?
Hi
Im coding an array of Student objects. It will build have options to a) add and b) remove elements (Students) from the array. Adding is straightforward enough but the problem for me is removing. I assume the steps i need re something like : Search for matching object in array Remove object and move everything after back one Shrink array size by 1 Problem is i believe you cant resize an array so maybe another array will be needed. Any suggestions? Thanks in advance. |
If you need to keep it sequential and compact in the array, and sized only large enough to hold the items within it, then you'd be better of with a second array. Simply iterate through the array that exists, and push onto the new array. If you hit the matching item to remove, then skip it, and keep going. If the end result is that no item was removed, than return the original, otherwise return the new.
This said, if you keep only an array large enough to hold a given number of items at the time, than its not really optimized. It's quite a bit of effort to copy an array into a larger one, than it is to leave unused space. Compare it to the add. The remove should be pretty much the exact opposite. If you create an array of current.length + 1, then add an item to it, then it makes sense to do the same with remove. If the add is not always incrementing the current array and creating a new one, than I'd suggest that the remove should not and instead simply setting the offset when found to null. |
Thanks for the advice.
Lets say for example after removing it i had to print it out. Code:
then remove element 1. I need to be able to go back and enter another student Maybe somehow when printing skip anything thats null?? but how will the add student function know where to put the student back if it doesnt free up the end? Maybe move all back one and leave the end null |
I think I see what you're getting at here.
Is this for an assignment? If so, you'll want to go over the instructions very carefully. If it specifies that add must append to an array or insert at the end, then yes you'll need to figure out the best way of shuffling back. If it does not specify this, and it does not specify a sort order for the array, than I'd suggest insertion via iteration to find the first null entry. I would perceive this currently as tracking a count of the known values, and adding based on the known number of records. You can shift back the array as well. Its a matter of determining what you want to remove, and then from that point on reassigning the values back by one (or going the other way and looking forward). You'll need to deal with the null insertion at the end though, otherwise it would replicate the last value in the list. So you can do this within a single loop iteration as well. If this isn't for an assignment, than you don't deal with arrays at all. You deal with a collection designed for volume insert/remove operations on it. Suggested collection types would be LinkedLists if order doesn't matter (ie in order only), or a Tree if order is important. There is no built in Tree ADT in Java though, so you'd need to write one (or find one online). Linked types are the most ideal since you don't need to do anything special to free up space. Simply remove the item, and insert wherever. |
We actually have a working example where we cant see the source code so i cant check 100% for sure what way it has to be done. Its definitely arrays alright as thats what where covering now.
Searching for the first null and putting the new entry in sounds like the most efficient way of doing it. Is there a way to just print out elements that are not null? I can send you the example if you want i just dont want to share it with every person who views this thread as its an assignment |
Yeah, printing them out is simply a matter of using an if:
PHP Code:
|
This did the trick.
Code:
int position = searchStudents(studentIDIn); |
| All times are GMT +1. The time now is 02:22 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.