How would I replace an item in a Collection by index?

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
The Collection object only has a Contains method, an Add method (which adds an object at the end of the Collection), and a Remove (by index) method.

So for example, if my Collection contains:
125
224
529
489

and I want to replace 529 with 5299, what would be the easiest way to do this? I really don't want to use a loop.
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
I'll go out on a limb and say you are talking about a Collection in Java.
Collection is an interface, not a class. Many classes implement the Collection interface. Try using something like ArrayList which adds the method: add(int index, E element)
 

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
A VB Collection, but I don't think that really matters. I'm just trying to think of an easy algorithm.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You're going to have to loop through to find the Index of the value you want to replace. Especially with such a basic(no pun intended) Data Structure.

Now, if your data structure stores the data in a sorted list, you can use a binary search O(logn) to find your index, faster then just searching the entire list O(n).

If your data structure is un sorted, you can use something like a Quick sort to sort it and then use the search to find it.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Crusty
You're going to have to loop through to find the Index of the value you want to replace. Especially with such a basic(no pun intended) Data Structure.

Now, if your data structure stores the data in a sorted list, you can use a binary search O(logn) to find your index, faster then just searching the entire list O(n).

If your data structure is un sorted, you can use something like a Quick sort to sort it and then use the search to find it.

What did you just get finished with sorting class.

So your idea is to use a sort that is n log(n) (can be as bad as n^2) then apply a lg(n) search? Why not just do a linear search and be done with it?

Also you and I for that matter have no idea how his data is stored. A binary search on a linked list takes n^2 operations and other data structures have no order and there for can't be sorted.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: smack Down
Originally posted by: Crusty
You're going to have to loop through to find the Index of the value you want to replace. Especially with such a basic(no pun intended) Data Structure.

Now, if your data structure stores the data in a sorted list, you can use a binary search O(logn) to find your index, faster then just searching the entire list O(n).

If your data structure is un sorted, you can use something like a Quick sort to sort it and then use the search to find it.

What did you just get finished with sorting class.

So your idea is to use a sort that is n log(n) (can be as bad as n^2) then apply a lg(n) search? Why not just do a linear search and be done with it?

Also you and I for that matter have no idea how his data is stored. A binary search on a linked list takes n^2 operations and other data structures have no order and there for can't be sorted.

Where did I say a linked list?
Did you even read my first sentence?

I specifically said he would have to perform a linear search with his current data structure.

Given the data he provided in the OP, or lack thereof, I was fishing at ideas to hopefully give him some insight to a new approach.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
If your collection is small or unsorted, do a linear search...

If your collection is large AND sorted, do a binary search for it.

If your collection has the potential to be large, use a binary search, but it must be sorted. Binary searches only work on sorted lists.

Unless you develop a tree algo.

I'm hoping you're asking how to find it efficiently for replacement, because I'm assuming replacing is a no-brainer, just like in C/C++.