🔒 Locked
Arrays...java
Advertisement
Original Post
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
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.
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.
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
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
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
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
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.
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;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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement