Help with Array.

BlitzMax Forums/BlitzMax Beginners Area/Help with Array.

Yue(Posted 2015) [#1]
Local int_array[,]=[{1},[{1}]


No achievement initiate a two-dimensional array at the time of his statement. Any suggestions .


col(Posted 2015) [#2]
Hi Yue,
for multidimensional arrays...
Local array:Int[10,5]
' or
' Local array[,] = New Int[10,5]
For Local i:Int = 0 Until 10
	For Local m:Int = 0 Until 5
		array[i,m] = m
	Next
Next



for arrays of arrays etc...
Local array:Int[][] = New Int[10]
For Local i:Int = 0 Until 10
	array[i] = New Int[5]
	
	For Local m:Int = 0 Until 5
		array[i][m] = m
	Next
Next

For Local a:Int = 0 Until 10
	For Local b:Int = 0 Until 5
		Print array[a][b]
	Next
Next



Yue(Posted 2015) [#3]
Hi, Hello I'm a little confunsed , when the collection is one-dimensional elements you can start with at the time declared that collection .In this case 'm trying to start it in two dimensions. It should be noted that I do is for learning purposes .
Local int_array[] =[1,2,3]



Yasha(Posted 2015) [#4]
The auto-array syntax isn't supported for multidimensional arrays - it's single-dimensional only. You need to set the elements individually in the multidimensional case.

You can use it to initialize an array-of-arrays, but that's not quite the same thing. (Personally I think array-of-arrays is more elegant design-wise - it might be slightly less efficient if you use a lot of them or your arrays are very big though.)


Floyd(Posted 2015) [#5]
Original example not really a two-dimensional array.

One dimensional looks like this

Local a[] = [1,2,3] ' three element array of integers


Pseudocode

 Local b[] = [thing1,thing2,thing2]  ' three element array of "things"


Sort of a two-dimensional array.

local c[][] = [ [1,2,3], [4,5,6] ]  ' two element array, each element is a three element array.



Yue(Posted 2015) [#6]
Thanks You Yasha And Floyd :)


col(Posted 2015) [#7]
oh wait! I'm using BlitzMax NG... so there's an issue there that needs to be fixed.

for regular blitzmax
The compiler needs to know the size of the array otherwise it doesn't know what size to build for you. A one dimensional array is easy as there is only one dimension. If you have an array with 12 values - hows would the compiler know what you want? [3,4] or [4,3] or [2,6] or [6,2] or [12,1] or [1,12]??


Bobysait(Posted 2015) [#8]
In blitzmax, a multi-dimentional array is in fact an array of object
Then if you want to initialize an array without filling it with values (auto-array), initialize it as a single array first, then initialize all cells



for ex: an 2 dimention array of random type
' let's suppose it's a [x,y] array of int
Local ar:Int[][]; 'here the array is just declared, but is Null


the syntax is obscure but it's actually what you should see (and how you should simplify it in your mind):
ar1:Object[] -> where "Object" is replaced with "Int[]"
so an array of "int[]" is "(Int[])[]

Then, when you understand how it really looks, you also understand why the first dimension initialized is actually the second
ar = new Object[12] replaced with and "Int[]" will be
ar = new Int[][12];


Now you HAVE TO understand that you just initialize an array of "Null" object
you HAVE TO initialize the Object (the Int[])
' make the length of X cells 8 -> you have to create an array for each cell
For local y:int = 0 until ar.length
   ar1[y] = new Int[8];
Next;



But ... as long as a 2 dimentional array is an array of object, you can have arrays of different length for each cell
For local y:int = 0 until ar.length
   ar[y] = new Int[Rand(1,15)];
Next



conclusion, an array is an Object and a multi-dimensional array is an array of Objects.

Hope it's clear enough.