[Gambas-user] Accessing the nth item in a collection

Ian Haywood ihaywood3 at ...626...
Wed Jun 4 14:11:18 CEST 2014


On Wed, Jun 4, 2014 at 9:59 PM, Tobias Boege <taboege at ...626...> wrote:

> Oh, I just saw the "added to" bit. That's not possible with AvlTree and also
> won't be with Collection.
>
> Then I suppose that you create a custom Collection class and make it record
> the first and last items added. For the general case of the n-th added item,
> you really need to sort your keys by the point in time they're inserted and
> that's plainly an array using the Add() method.
My first question is why not just use an Array full-stop: by
definition that is the structure for accessing
items by order.

> Generally, if you want to have the same data *sorted* in different ways (and
> you want "sorted" because otherwise you would need to loop through the data
> which you don't want), it sounds plausible that you need the data twice, but
> in diffent containers and sorted differently.
Correct.
if the "things" in question are Objects of any type, then internally
what's being held in the Array or Collection are pointers
to the objects, so extra memory use is minimal. In this case IMHO the
most graceful  and efficient solution is to have both an Array and a
Collection of the same objects.

If that doesn't work then I'd have a String[] holding the keys to the
collection, this means two lookups so a bit of a speed penalty (but
Gambas is fast at this stuff, I doubt you'd notice)

Ian




More information about the User mailing list