Array exeption in two lines...

BlitzMax Forums/BlitzMax Beginners Area/Array exeption in two lines...

BlitzProg(Posted 2007) [#1]
Local earth2:Int[][][]
earth2[1][1][1]=1


I get an unhandled exception.
whats wrong with it? What should i write instead of this?

Edit : i am quite new to blitzmax.


TomToad(Posted 2007) [#2]
Local earth2:int[2][2][2]
earth2[1][1][1] = 1

You need to initialize the size of the array before you can use it.


BlitzProg(Posted 2007) [#3]
Still not working

I am getting the Compile Error: Expression of type 'Int' cannot be invoked

edit :
Local earth2:Int[2,2,2]
earth2[1,1,1]=1

is that the only way to do it?


Who was John Galt?(Posted 2007) [#4]
You can do what you want, but the initialisation is a bit
more complicated. I guess an array initialiser only initialises one array per command.

Local earth2:Int[][]
earth2=New Int[] [2]

For n=0 To 1
	earth2[n]=New Int [2]
Next

earth2[1][1] = 1
DebugLog earth2[1][1]


I'll leave it as an exercise for the reader to extrapolate to 3D. I wouldn't bother with arrays of arrays unless you have a specific reason. Multidimensional arrays are less of a headache.


BlitzProg(Posted 2007) [#5]
Basically, i wanted to see if it was possible to work with arrays without having to set up the size.

Arrays of arrays seemed to be a more logical way to do things, but i think multidimentional arrays are easier to use :)

Edit, sorry for my bad english, i try to do my best.


Edit2:

Local earth2:Int[][]
earth2=New Int[] [2]

For n=0 To 1
	earth2[n]=New Int [2]
Next

earth2[1][1] = 1
DebugLog earth2[1][1]


Ok, fully understood how to use array of arrays, i guess there is no way to create arrays without setting up their initial size.


Grey Alien(Posted 2007) [#6]
I tried to init a multidimentional array a while back but it can't be done. You have to manually do it, sorry.


FlameDuck(Posted 2007) [#7]
Basically, i wanted to see if it was possible to work with arrays without having to set up the size.
It isn't. An Array, by definition, always has a known size. Depending on what you're trying to achieve, try using Lists or Graphs instead.


BlitzProg(Posted 2007) [#8]
Your help was really apprecied, it allowed me to define things without making the compiler angry.

the following is just some training i wanted to do, could allow me to create selfmade strange things :)
Just to show you its correctly eaten by the compilater.

Global Xindex:Int=0
Global X:Int[][][][]

Function createXarray()	
	X=New Int[][][] [10]
    X[Xindex] = createX(2, 2, 2, 0, 0, 0, 2, 2, 2);
    X[Xindex] = createX(2, 2, 2, 0, 2, 0, 2, 2, 2);
    X[Xindex] = createX(2, 2, 2, 0, 0, 0, 2, 2, 2);
    X[Xindex] = createX(1, 1, 1, 1, 1, 1, 0, 0, 0);
    X[Xindex] = createX(1, 1, 1, 1, 2, 1, 0, 0, 0);
    X[Xindex] = createX(1, 1, 1, 1, 1, 1, 0, 0, 0);
    X[Xindex] = createX(0, 0, 0, 2, 2, 2, 0, 0, 0);
    X[Xindex] = createX(0, 0, 0, 2, 0, 2, 0, 0, 0);
    X[Xindex] = createX(0, 0, 0, 2, 2, 2, 0, 0, 0);
    X[Xindex] = createX(0, 0, 0, 0, 0, 0, 0, 0, 0);
End Function

Function createX:Int[][][](a:Int, b:Int, c:Int, d:Int, e:Int, f:Int, g:Int, h:Int, i:Int)
	Local rArray:Int[][][]=New Int[][][4]
    rArray[0] = [[a,b,c], [d,e,f], [g,h,i]];
    rArray[1] = [[d,e,f], [g,h,i], [a,b,c]];
    rArray[2] = [[a,b,c], [g,h,i], [d,e,f]];
    rArray[3] = [[g,h,i], [d,e,f], [a,b,c]];
    Xindex=Xindex+1
    Return rArray;
End Function



Jesse(Posted 2007) [#9]
you can init a blank multi dimensional array on the run:
Global array:Int[,,]

array = Createarray(10,10,10)
Print array.length
array = createarray(20,20,20)
Print array.length
Function createarray:Int[,,](x:Int, y:Int, z:Int)
	Return New Int[x,y,z]
End Function  

what you can not do is resize it and keep the data. but I am shure a work around can be easily implemented.