help in search&replace for ArrayList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have an arrayList that holds an ArrayObject with (Qty & ItemCode).
e.g.
arrayList.Add(New ArrayObject(Qty, ItemCode)

However, I want to search the arrayList and check if the ItemCode already
exist on the arrayList, then simply increase the Qty by 1.

For example:
I have in my arraylist:
index 0, object {1, "1111"}
index 1, object {1, "2222"}
index 2, ojbect {3, "7777"}

Now i want to add another object say {4, "2222"} to the arrayList, but
instead of adding it as NEW item to the list, I want to able to find the
index and replace the Qty, so index 1, will now looks like {5, "2222"}.

I hope u see what i mean....
Any help is appreciated, Many thanks in advance !
Jon
 
just wonder are there any faster method to search like using indexOf? and
instead of looping each item and compare the ItemCode?

thanks
 
If your list is sorted, you can use a Binary Search to get the item very
quickly. If it isn't sorted, you might want to consider using a hashtable
instead of an array, using the first number as the key. But this will only
work if you think your numbers are unique.
 
Or use a Generic Dictionary class. Something like this:

Dim MyObjects As New List(Of String, Integer)

If MyObject.Keys.Contains("2222") Then
Dim newQty As Integer = MyObjects("2222") + 5
MyObjects("2222") = newQty
Else
MyObjects.Add("2222", 5)
End If
 
Back
Top