Link to home
Start Free TrialLog in
Avatar of nike_golf
nike_golfFlag for Afghanistan

asked on

Easiest way to replace item in array...

Is there a fast and efficient way to replace an item in a one dimensional array? or do I need to loop through the items and just replace the item that way?

NG,

ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nike_golf

ASKER

duh... thx.
If you are looking for a fast way to determine the INDEX of an item in an array...then No.  You have to loop over it to find the right spot that you want to overwrite.

If you need a fast way to do LOOKUPS then use a Collection instead of an Array.
Sorry, was jim horns answer what you were looking for?

If the array is sorted, then a binary tree search is a good way of quickly finding an item

To remove an item is harder

To swap an Item with another part of the array requires a temporary variable of the same type
  e.g.

tmpString = aitem(2)
aitem(2) = aitem(4)
aitem(4) = tmpString

Or are none of these the type of thing you are asking about?
Or if you want to replace all items of a certain value...

That either requires a loop (easy & fast on small arrays)
or a binary search on a sorted array, followed by a loop (harder but very fast for large arrays)
Senial moment on my behalf... thanks guy's..