data containers
Moritz
mpipahl at gspgmbh.com
Sat Jul 5 06:30:30 PDT 2008
Thanks again, torhu. I understand multidimensional arrays (at least I
think so :-) ), and that these arrays contain pointers to other arrays.
I thought the .length() operation would make the array static in its
size, thats why I didnt try this earlier.
But after reading what you wrote, I still dont see why I cant go without
the length(), by just using ~=, like:
ScreenElement[][] layer_map;
int add_element(ScreenElement new_element, uint layer)
{
layer_map[layer] ~= new_element;
}
You wrote setting the length and using ~= both increase the size of the
array, but I get an ArrayBoundsError when using the ~=.
And thank you all for your help so far!!!!
torhu schrieb:
> Moritz wrote:
>> Thank you torhu, I finally managed to get something into this array! :-)
>>
>> But I still dont understand if and how I can add a new ScreenElement
>> array to the layer map *without* setting its size in advance(because I
>> want to remain as generic as possible).
>
> I'm wondering if you're aware what a two-dimensional array really is.
>
> When you do this:
>
> ScreenElement[][] layer_map = new ScreenElement[][5];
>
> layer_map.length will be 5. But the lengths of the arrays in the second
> dimension will all be zero. For example, layer_map[0].length and
> layer_map[3].length will be zero. So you have allocated space for 5
> arrays of ScreenElements, but no space for the ScreenElements
> themselves. Just 5 array references that don't have any space to refer
> to yet.
>
> I hope this makes sense to you.
>
>>
>> And another question: Can I modify the size of an array after I set it
>> manually?
>
> Sure, these two will work anytime:
>
> array.length = whatever; // set length to whatever
>
> array ~= stuff; // increase length by 1, add 'stuff' at end
>
More information about the Digitalmars-d-learn
mailing list