Sign in to follow this  
Nidhi

Arrays...java

Recommended Posts

Hey guys I want to perform a deletion operation on arrays but I have no clues about how should I proceed with it .If an item in the array matches with the parameter passed how am I supposed to delete it?....can anybody help me out with the algorithm for this deletion job.It's a java project. Thanks, -Nidhi

Share this post


Link to post
Share on other sites
There are 2 easy ways to delete an element from an array. One is to mark the element in some way so you know it has been deleted. Another is to move every element after the deletee back to occupy its space.

For the first method, remember that its fairly easy to mark something as deleted by setting it to null, provided of course that the rest of your program doesn't assume that every element in the array has to point to something valid.

Share this post


Link to post
Share on other sites
hey rip-off


ok if I use the second method..Do I have to copy the value of all the old elements ,which were before the element that I want to delete, to a new array and then move all the elements which were after the element to be deleted one step ahead.And the new array should be one less than the original array


I'm confused

Share this post


Link to post
Share on other sites
hey rip-off


ok if I use the second method..Do I have to copy the value of all the old elements ,which were before the element that I want to delete, to a new array and then move all the elements which were after the element to be deleted one step ahead.And the new array should be one less than the original array


I'm confused

Share this post


Link to post
Share on other sites
Quote:
Original post by Nidhi
Seriously I don't know anything about vectore coz I haven't used it.I need a simple approach to this operation..Please help me out!


Are you doing this for use in a program, for learning about arrays or as homework?

Share this post


Link to post
Share on other sites
Well not as a homework..Actually we(me and some members of java users group on campus)are stuck on dis thing.We are working on a project that requires the use of deletion operation on arrays!

Share this post


Link to post
Share on other sites
Quote:
Original post by Nidhi
Well not as a homework..Actually we(me and some members of java users group on campus)are stuck on dis thing.We are working on a project that requires the use of deletion operation on arrays!


If the project is not about learning how to use arrays, then I would recommend using a class like ArrayList or Vector to hold your data, as these allow you to easily remove items.

If demonstrating array knowledge is part of the project then we cannot help you directly, however if you post some code we may be able to hint you to where you are going wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nidhi
I want to perform a deletion operation on arrays but I have no clues about how should I proceed with it.

Can you be a little more specific:

What items are stored in the array? primitive type or reference type?

Do you want to remove by index (say delete the fifth element) or by element (say delete the string "hello world")? In the second case, do you want to remove the first occurance, or all of them?

Does the order matter? So is it ok to rearrange the items in the array if that is more efficient?

Just to give you an example, the following code assumes an array of Strings named array and an integer pos which indicates the element you want to remove:


final String[] temp = new String[array.length-1];
System.arraycopy(array, 0, temp, 0, pos);
System.arraycopy(array, pos+1, temp, pos, array.length-pos-1);
array = temp;

Share this post


Link to post
Share on other sites
Quote:
Original post by Nidhi
If an item in the array matches with the parameter passed how am I supposed to delete it?

Oh, didn't read that the first time. Assume array is an array of Strings, and item is the String to be removed.


List<String> list = Arrays.asList(array);
while (list.remove(item));
array = list.toArray(new String[list.size()]);


This will remove every occurance of the item in the list. If you only want one deletion, replace the second line with "list.remove(item);".

Now if this is some kind of homework, no teacher in the world would accept that as a solution, because you're supposed to do the searching for the item yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this