Slices, appending to arbitrary position
Jakob Ovrum
jakobovrum at gmail.com
Mon Dec 30 10:35:15 PST 2013
On Monday, 30 December 2013 at 18:19:54 UTC, Dfr wrote:
> This simple example:
>
> string[] a;
> a[10] = "hello";
>
> Gives me: core.exception.RangeError: Range violation
>
> I know about this way:
>
> string[] a;
> a = new string[11];
> a[10] = "hello";
>
> But what if i need do this many times with the same array like
> this:
>
> a[10] = "a";
> ...
> a[1] = "b";
> ..
> a[1000] = "c";
>
> If i will call "a = new string[11]" all those many times, isn't
> this will be inefficient ? Also it will clear all previous
> contents of "a", which is not suitable.
Arrays are contiguous chunks of memory. If you wanted to store
"c" as the 1000th element in an array of strings, that array
needs to have room for 1000 elements.
Perhaps what you want is an associative array:
----
string[uint] aa;
aa[10] = "a";
aa[1] = "b";
aa[1000] = "c";
writeln(aa[10], aa[1], aa[1000]);
----
More information about the Digitalmars-d-learn
mailing list