Error When Declaring Arrays of Arrays

BlitzMax Forums/BlitzMax Programming/Error When Declaring Arrays of Arrays

SebHoll(Posted 2007) [#1]
Why is it not possible to set the dimensions of an array of an array while declaring it?

SuperStrict
Const FIRST_DIMENSION% = 8, SECOND_DIMENSION% = 16
Print "Attempting to create array..."
Local tmpArrayOfArray:Byte[FIRST_DIMENSION][SECOND_DIMENSION]
Print "Completed!"
Attemting to compile the above example generates a...



It's really easy to do in C, so I thought it would work in BlitzMax too!

Any ideas?


Diordna(Posted 2007) [#2]
SuperStrict
Const FIRST_DIMENSION% = 8, SECOND_DIMENSION% = 16
Print "Attempting to create array..."
Local tmpArrayOfArray:Byte[FIRST_DIMENSION,  SECOND_DIMENSION] '<-- NOTICE THE COMMA.
Print "Completed!"

Hope it helps!


degac(Posted 2007) [#3]

' Create empty array of arrays
Local max1:Int=10,max2:Int=10
Local ClearField:Int[][]
' Resize main array to hold 10 subarrays (0...9)
ClearField = ClearField[..max1]
' Resize all subarrays to hold 10 elements (0...9)
For Local i = 0 Until max1
ClearField[i] = ClearField[i][..max2]
Next
Print ClearField.length ' Prints the size (number of elements) of the main array
Print
For Local m = 0 Until max1
Print ClearField[m].length ' Prints the size of a subarray at each index of the mainarray
Next
Print ' Print an empty line
' Printing the contents of all elements
For Local j = 0 Until max1
Print
For Local k = 0 Until max2
Print ClearField[j][k]
Next
Next
' Print an empty line
Print
' Resizing only 1 subarray (the one at main array index 5)
ClearField[5] = ClearField[5][..20]
' Printing the size of all subarrays
For Local l = 0 Until max1
Print ClearField[l].length
Next

Taken from here http://www.blitzbasic.com/Community/posts.php?topic=46944#522206

It seems that Bmax can't auto set the size of an array of arrays.


SebHoll(Posted 2007) [#4]
@ Diordna:
SuperStrict
Const FIRST_DIMENSION% = 8, SECOND_DIMENSION% = 16
Print "Attempting to create array..."
Local tmpArrayOfArray:Byte[FIRST_DIMENSION, SECOND_DIMENSION] '<-- NOTICE THE COMMA.
Print "Completed!"

Thanks, but this isn't quite the same as what I'm looking for. I specifically need an array of arrays (as oppose to a multi-dimensional array) as I want to be able to separate the variable into sets of arrays (if you see what I mean :-P).

@degac:
It seems that Bmax can't auto set the size of an array of arrays.

OK, thanks for confirming that for me. I have been using a similar work-around that involved using slices to resize arrays when they have been previously initialized but it isn't very tidy. Still, if there isn't any other way to do it...


Dreamora(Posted 2007) [#5]
You don't need to slice

You could simply just initialize it correctly using new on the "outer" array and initializing each sub array with new as well instead of using slicing.

Arrays of arrays must be manually initialized on each "sub array" (something that is not needed with 2D arrays, which is why they can work like that)


SebHoll(Posted 2007) [#6]
You don't need to slice

You could simply just initialize it correctly using new on the "outer" array and initializing each sub array with new as well instead of using slicing.

Could you please post an example?

Thanks


Azathoth(Posted 2007) [#7]
Like this?

SuperStrict
Const FIRST_DIMENSION% = 8, SECOND_DIMENSION% = 16
Print "Attempting to create array..."
Local tmpArrayOfArray:Byte[][FIRST_DIMENSION]

For Local i:Int=0 To FIRST_DIMENSION-1
	tmpArrayOfArray[i]=New Byte[SECOND_DIMENSION]
Next
Print "Completed!"