How do you set the dimension of an array when you don't yet know it at compile time?
For instance: byte[][] a = new byte[size][size]; The compiler does not allow it. How am I supposed to initialize the grid? Manually?
byte[] a1;
for (int i; i < size; i++) {
a1 ~= 0;
}
byte[][] a2;
for (int i; i < size; i++) {
a2 ~= a1;
}
Please tell me there is a simpler way.
Edit: this also works, but it is still hopelessly primitive, and slow
byte[][] a3;
a3.length = size;
for (int i; i < size; i++) {
a3[i].length = size;
}
Don't going into depths, here is an example of initializing multidimensional dynamic array in D:
auto a = new int[][](4, 4);
Edit: here goes more complete example (showing that you can initialize array during runtime to avoid confusion):
int x = 3, y = 4, z = 5;
auto a = new byte[][][](x, y, z);
Stdout(a[0][0].length).newline; // prints 5
a[0][0].length = 10;
Stdout(a[0][0].length).newline; // prints 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With