This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing from an array

 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all
Can somebody give me the best way from performance point of view, how to delete an element from an array.
THanks
kundan
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Performance forum...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well - without wholes wanted in the array, use ArrayList, and:

arrayList.remove (3);

and

arrayList.toArray ();

if someone needs an array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic