Hi all,
As I know, we can loop and compare each item of ArrayCollection to get correct item we want. Obveriously, the performance will get bad and it's not a wise method to search. Can we create a index for the key field of each item? Or is there any other class providing such functionality?
Thanks,
Is it possible to search...you can use
public function getItemIndex(item:Object): int
to see if the item exists and at what index
Thanks,
Gaurav Jain
Flex SDK Team
http://www.gauravj.com/blog
Is it possible to search...Thanks for your reply but that's not what I want.
The getItemIndex()
method cannot take a parameter with only a subset of the fields in the item being serched for; this method always searches for an item that exactly matches the input parameter. -- copy from online help
Actually, if the key field is equal, it can be considered the same. It's not diffiicult to build an index by myself, but if there's existing way it'll be definitely great.
Many thanks and best regards,
When you say ''an index'' are you talking as a in a database table index??I.e. something that can quicken a search?
I am trying to understand your desire and I think what I am hearing is that you have an ArrayCollection contain a number of Objects. When you want to find a particular Object, you have found that one way to locate the desired instance is to walk through each element of the ArrayCollection and ask ''does this element equal the key of the object I am looking for?''. I think you are concerned by the amount of time that this may take.
Going back decades to my computing science knowledge, this would be an O(n) algorithm where n is the number of elements in the array.
Now, if there were an ''index'' (ala databases) on the data, then if this index were Sorted, you would be able to find your match in O(log n)
You may pay an O(n log n) cost to sort the index when the data structure is created but if you have a large number of searches then it may pay for itself.
I am not aware of any function that will support you on this out of the box (but that doesn't mean there isn't, only that I don't know about it.
I would also truthly ask yourself ''is it worth the cost?''. How many elements will you have in a typical array??How often do you anticipate locating an entry in that array?
Neil
Hello Neil,
What a great answer you give! You point out my mistake exactly and elaborate my question quite well. There's no need to pay O(n) maintaining index instead of searching with O(n) also. How shame that I throw those knowledage away.
But wait. To my desire, if the key fields are equal then I think they are equal. To getItemIndex, it will compare the whole object. I remember in JAVA I can override equal() to achieve. But how about this in Flex?
Best regards,
if your keys are going to unique, then may be you can use flash.utils.Dictionary and store data as key/value pairs.
-Gaurav
No comments:
Post a Comment