[Info-vax] Simple Pascal question
Arne Vajhøj
arne at vajhoej.dk
Thu Sep 5 13:30:44 EDT 2024
On 8/5/2024 8:34 AM, Michael S wrote:
> On Sun, 4 Aug 2024 21:00:35 -0400
> Arne Vajhøj <arne at vajhoej.dk> wrote:
>> On 8/4/2024 8:09 PM, Dan Cross wrote:
>>> In article <v8o4h8$2ut3$1 at dont-email.me>,
>>> Arne Vajhøj <arne at vajhoej.dk> wrote:
>>>> Like:
>>>>
>>>> public class FlexArray {
>>>> private static void dump(int[] a) {
>>>> for(int v : a) {
>>>> System.out.printf(" %d", v);
>>>> }
>>>> System.out.println();
>>>> }
>>>> public static void main(String[] args) throws Exception {
>>>> int[] a1 = { 1 };
>>>> int[] a2 = { 1, 2 };
>>>> int[] a3 = { 1, 2, 3 };
>>>> dump(a1);
>>>> dump(a2);
>>>> dump(a3);
>>>> }
>>>> }
>>>
>>> Java arrays are more like the aforementioned slices.
>>
>> I don't think so.
>>
>> Java does not have anything like slices.
>>
>> C# does.
>>
>> C# Span is similar to slices. But C# Span and C# array are far from
>> the same.
>
> Pay attention that despite being designed (or at least brought to
> public) in this century, C# originally lacked slices.
> They were added relatively recently, in v. 8.0, and referred in c# docs
> as "Indices and ranges".
I thought it was C# 7.2.
It is important to note that C# Span and Range are
a little bit different than similar syntax in some of
the newer native languages supporting slice.
int[] a = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
Span<int> s1 = a[1..3];
does not create a span/slice/view of the second and third
element of a - it allocates a new array of length 2,
copy the second and third element of a to it and
create a span/slice/view of the new array.
Span<int> s2 = ((Span<int>)a).Slice(1, 2);
Span<int> s3 = (new Span<int>(a)).Slice(1, 2);
Span<int> s4 = new Span<int>(a, 1, 2);
all create a span/slice/view of the second and third
element of a.
Arne
More information about the Info-vax
mailing list