Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 05, 2010 setting array dimensions at runtime | ||||
---|---|---|---|---|
| ||||
Hello, I've been wondering what the easiest way is to set the dimension of an array during runtime. You can't write byte[][] a = new byte[size][size]; because the compiler will give an error. The only thing I've been able to think of is byte[][] a; a.length = size; for (int i; i < size; i++) { a[i].length = size; } But it's slower (and less convenient) than writing byte[][] a = new byte[9][9]; |
December 05, 2010 Re: setting array dimensions at runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to user |
> The only thing I've been able to think of is
>
> byte[][] a;
> a.length = size;
> for (int i; i < size; i++) {
> a[i].length = size;
> }
Well, you can do at least:
auto a = new byte[][size];
foreach (ref row; a)
row = new byte[size];
Matthias
|
December 05, 2010 Re: setting array dimensions at runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Walter | On 5-12-2010 19:20, Matthias Walter wrote:
>
>> The only thing I've been able to think of is
>>
>> byte[][] a;
>> a.length = size;
>> for (int i; i< size; i++) {
>> a[i].length = size;
>> }
> Well, you can do at least:
>
> auto a = new byte[][size];
> foreach (ref row; a)
> row = new byte[size];
That works.
So I can write
byte[] a = new byte[size];
and
byte[][] a = new byte[][size];
but not
byte[][] a = new byte[size][];
or
byte[][] a = new byte[size][size];
let alone
byte[size][size] a;
BTW, somebody on stackoverflow just posted an alternative
that comes closest to what I was looking for.
byte[][] a = new byte[][](size, size);
I saw this notation before but I can't remember where.
Hey, I love D but it can be pretty confusing sometimes :)
Thanks
|
December 05, 2010 Re: setting array dimensions at runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to user | On 2010-12-05 19:41:50 +0200, user@domain.invalid said:
> Hello,
>
> I've been wondering what the easiest way is to set
> the dimension of an array during runtime.
>
> You can't write
>
> byte[][] a = new byte[size][size];
>
> because the compiler will give an error. The only
> thing I've been able to think of is
>
> byte[][] a;
> a.length = size;
> for (int i; i < size; i++) {
> a[i].length = size;
> }
>
> But it's slower (and less convenient) than
> writing
>
> byte[][] a = new byte[9][9];
The syntax might seem a bit misleading, but you can create multidimentional arrays like this:
int[][] foo = new int[][](5,10);
foo[4][9] = 31337;
this also works for single-dimention arrays (e.g int[] foo = new int[](size);)
|
December 05, 2010 Re: setting array dimensions at runtime | ||||
---|---|---|---|---|
| ||||
Posted in reply to user | On Sunday 05 December 2010 09:41:50 user@domain.invalid wrote:
> Hello,
>
> I've been wondering what the easiest way is to set
> the dimension of an array during runtime.
>
> You can't write
>
> byte[][] a = new byte[size][size];
>
> because the compiler will give an error. The only
> thing I've been able to think of is
>
> byte[][] a;
> a.length = size;
> for (int i; i < size; i++) {
> a[i].length = size;
> }
>
> But it's slower (and less convenient) than
> writing
>
> byte[][] a = new byte[9][9];
auto a = new byte[][](9, 9);
is the way to do it. Otherwise, I believe that you're trying to create a dynamic array of static arrays or somesuch. I'm not sure exactly what the problem is. However, if you just always put the array size in the parens rather than in the brackets when creating an array, then it works correctly. Setting the length in a loop like that (or creating inner arrays with new) is best when you want the inner arrays to be of different length. But when you want them to be the same length, then putting the sizes in the parens in the correct way to go.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation