[Gambas-user] Would an ArrayList datatype be helpful for gambas users?

BB adamnt42 at gmail.com
Mon Sep 11 18:10:16 CEST 2023


On 12/9/23 1:07 am, T Lee Davidson wrote:
> On 9/11/23 04:18, Gianluigi wrote:
>> You made me want to check out Bjarne Stroustrup's book.
>>
>> Slice is well explained, unfortunately it is 'way' beyond my mental 
>> capabilities.
>>
>
> I have to doubt, Gianluigi, that slices are beyond your comprehension. 
> They are actually not complex. Perhaps that book's explanation was 
> rather convoluted.
>
> A slice is, simply stated, a limited view of an array.
>
> Suppose we could do this:
>
> Dim aStrings As String[] = ["one", "two", "three", "four", "five", 
> "six", "seven"]
> Dim aSlice As Slice = aStrings.Slice(2, 3) ' Syntax equals 
> Slice(startIndex, Length) [ Except in Python :-\ ]
>
> The slice's view of the underlying array would consist of only the 
> values ["three", "four", "five"] and would be indexed starting at zero.
>
> Print aSlice[1] ' Would print "four"
>
> Since a slice is not a copy of a subset of an array (but holds a 
> reference to the underlying array), changing a value in the slice also 
> changes the referenced value in the array:
>
> aSlice[1] = "nine"
> ' aStrings now equals ["one", "two", "three", "nine", "five", "six", 
> "seven"]
>
> See? Simple. (But, I may not have explained it very well either.)
>
>
and presumably, since they are "just" a reference into the array, you 
could do adds or inserts into the array using either the array or the 
slice and they would stay synchronised.

I am not sure what extracts or removes would be capable of mucking up 
though. Using Lee's sample

aSlice = aStrings.Slice(5,2)
aStrings.Remove(5,2) 'Where would aSlice be pointing to now? Would it be 
null or an empty String[]?

or

aStrings.Extract(4,-1) ' Where would aSlice be pointing to now? Would it 
be null or an empty String[]?

I think they have to be an empty string array, but the reference into 
the original array must be destroyed.

b




More information about the User mailing list