Jump to content

  • Log In with Google      Sign In   
  • Create Account


Remove Element From an Array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 obizues   Members   -  Reputation: 176

Like
0Likes
Like

Posted 15 September 2012 - 06:01 PM

I'm trying to figure out how I can remove an element from a normal array (not ArrayList) in Java. I tried by just setting the array at the particular index to null, but that's apparently not what he wanted.

My only other guess is maybe try to create a new array, use arraycopy from starting at 0 until one less than the element being removed, and then then copying the same array from one element after the one being used to the end of the array you just created.

Then reference the old array to the new array and end the method.

Any thoughts?

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 7981

Like
0Likes
Like

Posted 15 September 2012 - 08:26 PM

Does order need to be conserved in the array? Because if not, you can simply swap the element to delete with the last element in the array, and cut the last element off (by resizing the array or doing an arraycopy on a new array). This is really efficient but if your array was sorted for instance it will no longer be.

If the order has to be preserved, you want to set the element to delete to null, and bring every element after that down a notch, and then resize the array by one less element (or again, just arraycopy, which is more efficient at this point since block memory copy is faster than walking through the array, but if you have a lot of elements to delete or if you need to go through the array to find which ones to delete, you may be better off sweeping through the array backwards and pushing everything down as needed).

So if you need to delete element at position P in an array of N elements, you would create a new array of N - 1 elements, and:
- arraycopy the first P - 1 elements of the old array into the beginning of the new array
- arraycopy the elements from index P + 1 to N - 1 of the old array into the new array at index P
(assuming zero-based indices)

Edited by Bacterius, 15 September 2012 - 08:28 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Verik   Members   -  Reputation: 260

Like
0Likes
Like

Posted 17 September 2012 - 03:26 PM

It also sounds like you want to do things that arrays are not very good at. Have a look at the List interface and some of the implementations. LinkedList or ArrayList could be just what you are looking for. Both support the immensely popular remove(int index) method.

#4 Glass_Knife   Moderators   -  Reputation: 3228

Like
0Likes
Like

Posted 18 September 2012 - 06:59 AM

Is there a reason to just do it with an array instead of a Collection class?

Since the array has a length property, setting an element to null doesn't resize the array.
The only way to shrink the array is to create a new one of the smaller size and copy
the data.

If you are using the array to store the data, but you have the data array hidden in a class,
then you could use null, keep track of the size yourself, and re-arrange stuff yourself.
I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#5 !Null   Members   -  Reputation: 380

Like
0Likes
Like

Posted 18 September 2012 - 07:39 AM

if you want arrays of Changing sizes, then a fixed array just isn't going to cut it.

if you needing to cut out elements in unpredictable places and then shrink the size.
consider linked lists.

with linked lists you can remove and element and then change the link to the next element:

<el1> -> <el2> -> <el3> -> <el4> -> <el5>

<el1 -> <el2> -> <el4> -> <el5>

That is a pretty vague example but you get what I mean. figure out what exactly the person you are doing this for wants exactly.

Edited by !Null, 18 September 2012 - 07:39 AM.

/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS