Dinamyc arrays

Salih Dincer salihdb at hotmail.com
Sun Sep 17 17:52:59 UTC 2023


On Sunday, 17 September 2023 at 17:15:34 UTC, Timofey wrote:
> I`ve just started learning d and have a question.
> What should I write to set dinamyc rectangular array length in 
> both dimentions?
> For example, I have declareted an array:
> ```   int[][] matrix;```
> and want set it as n*n matrix.
> Thanks


There are many ways to do this, but I can quickly show you two:

```d
import std.stdio;
void main()
{
     int[][] matrix;
     enum n = 6;

     // method 1:
     //matrix = new int[][](n, n); /*

     // method 2:
     matrix.length = n;
     foreach(ref e; matrix)
         e.length = n;//*/
     
     matrix.writeln;
}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list