arrays in DMD V2

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 18 22:55:35 PDT 2014


On 04/18/2014 08:47 PM, steven kladitis wrote:
> Thanks, I am trying to understand what I am doing. The docs seem unclear
> to me on how to initialize these.  I think there are three ways.
>   with brackets , the other with foreach or direct assignments.
> -- here is a longer version
> -- uncomment out the assignments to see the errors I get.
> -- I am trying to understand arrays.

I like to differentiate between arrays and associative arrays. Although 
its name has "array" in it, an associative arrays is actually a hash table.

First, let me repeat Jesse Phillips's suggestion: Do not use the C 
syntax. Here is the D syntax: T[N] means "an array of N objects of type T."

So, the following is an array of 2 ints:

     int[2] arr;

When the size of the array is specified like that, it is a fixed-length 
array.

There are dynamic arrays where the actual array is maintained and owned 
by the runtime. Such arrays are accessed by a slice. A slice is defined 
similar to a fixed-length array but the size is missing:

     int[] arr;

An associative array (AA) is defined by the syntax ValueType[KeyType]. 
So, the following is an AA mapping strings to doubles:

     double[string] table;

Here are three chapters on these topics:

   http://ddili.org/ders/d.en/arrays.html

   http://ddili.org/ders/d.en/slices.html

   http://ddili.org/ders/d.en/aa.html

Ali



More information about the Digitalmars-d-learn mailing list