|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
help in search&replace for ArrayListI 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. 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 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 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
.exe from within a form
Help trimming off filename from path How To Create Login Password Protected Database clearing all the lables on a form in vb.net 2005 Attempted to read or write protected memory Saving a data table to a MS Access database myMAPISession.SignOn() Help! Something adds 1 hour to my datetimes! :-S Troubled saving a clipped region from an image to disk as transparent Can VB 2003 use .net 2.0 framework? |
|||||||||||||||||||||||