aspose file tools*
The moose likes Performance and the fly likes Removing from an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Removing from an array" Watch "Removing from an array" New topic
Author

Removing from an array

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI all
Can somebody give me the best way from performance point of view, how to delete an element from an array.
THanks
kundan


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Performance forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Just to clarify a bit, does this include searching for the item? If so, is the array sorted? When something is removed from the middle, do the components further down the array all need to slide over a notch, preserving the order, or does anything need to fill in the new empty space?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



http://home.arcor.de/hirnstrom/bewerbung
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stefan Wagner:


Well, yeah, this is certainly the fastest. Wether it's actually doing what you want depends on your answers to Dirk questions, though...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI Derk
Yes order need to me maintained and the array is sorted.
What can be the solution then.I dont want to go with null option.
THanks
kundan
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Do you even need to use an array? Sounds like a TreeSet may be worth considering here (unless you need to allow duplicates). Alternately, perhaps a LinkedList, or even ArrayList. Depends what else you're doing with the data. If you must use an array, then you should probably use System arraycopy() to shift the latter part of the array one slot to the left, as efficiently as possible. You'll have a null at the end, but that's the price of using an array, I think. Unless you want to recopy the whole array to another which is one element smaller, which seems rather wasteful from a performance perspective.


"I'm not back." - Bill Harding, Twister
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I suspect the solution is different depending on how many elements you want to remove. I would add "how many elements have to be removed" to Dirk's questions. If the number to be removed is a large fraction of the total, building a new array is probably fastest.

Bill
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

well - without wholes wanted in the array, use ArrayList, and:

arrayList.remove (3);

and

arrayList.toArray ();

if someone needs an array.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Removing from an array
 
Similar Threads
convert an array of String to char
WA #1.....word association
How to sort Arraylist without Sort method
Date difference, with the table containing null values for date
Array item selection