Home All Groups Group Topic Archive Search About

help in search&replace for ArrayList

Author
3 May 2006 1:29 PM
Jon
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

Author
3 May 2006 1:45 PM
Jon
just wonder are there any faster method to search like using indexOf? and
instead of looping each item and compare the ItemCode?

thanks
Author
3 May 2006 2:10 PM
Fred Hedges
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.



Show quoteHide quote
"Jon" <J**@discussions.microsoft.com> wrote in message
news:5144D86B-72A7-4456-953A-E108DCB46A6C@microsoft.com...
> just wonder are there any faster method to search like using indexOf? and
> instead of looping each item and compare the ItemCode?
>
> thanks
Author
3 May 2006 3:21 PM
tony lock
If you are using 2005 a KeyedCollection would make the task simple

Show quoteHide quote
"Jon" wrote:

> 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
Author
3 May 2006 6:14 PM
Chris Dunaway
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