Two dimensional arrays aren't resizing?
Monkey Forums/Monkey Programming/Two dimensional arrays aren't resizing?
| ||
Here's a little trivial issue that I just ran into; I can't seem to resize two dimensional arrays. I recreated a minimal version of the problem: Strict Import mojo Class Test Extends App Field array2D%[][] Method New() Local array1D%[0] Local array2D%[][] array2D = [array1D, array1D] Print("Length before resize: " + array2D[0].Length()) array2D[0].Resize(array2D[0].Length()) Print("Length after resize: " + array2D[0].Length()) End End Function Main:Int() New Test() End If anyone knows about a better way to go about doing this, please don't hesitate to modify the code, repost, and constructively criticize me. ;) Thanks in advance, - Brandon. |
| ||
Ah, the weekly array question. Your problem is that you don't understand how Monkey arrays are defined. Please read http://monkeycoder.co.nz/Community/posts.php?topic=822 and see if that clears it up for you. |
| ||
Allow me to try again after taking a look through that post:Strict Import mojo Class Test Extends App Field array2D%[][] Field array1D%[] Method New() array2D = [New Int[0], New Int[0]] Print("Length before resize (2D): " + array2D[0].Length()) array2D[0].Resize(5) Print("Length after resize (2D): " + array2D[0].Length()) array1D = New Int[0] Print("Length before resize (1D): " + array1D.Length()) array1D.Resize(5) Print("Length after resize (1D): " + array1D.Length()) End End Function Main:Int() New Test() End I'm still getting the same results, no matter how many dimensions in use. Am I still defining them wrongly somehow? Thanks in advance, - Brandon. |
| ||
I's partly OK. ;) because you call Resize method but you forgot on re-assing it back to array. Documentation: Method Resize:Array( newLength ) Copies the elements of the current array into a new array of length newLength, and returns the new array. Last few words are important to you. Strict Import mojo Class Test Extends App Field array2D%[][] Field array1D%[] Method New() array2D = [New Int[0], New Int[0]] Print("Length before resize (2D): " + array2D[0].Length()) array2D[0] = array2D[0].Resize(5) Print("Length after resize (2D): " + array2D[0].Length()) array1D = New Int[0] Print("Length before resize (1D): " + array1D.Length()) array1D = array1D.Resize(5) Print("Length after resize (1D): " + array1D.Length()) End End Function Main:Int() New Test() End result: Length before resize (2D): 0 Length after resize (2D): 5 Length before resize (1D): 0 Length after resize (1D): 5 |
| ||
Whoops, my mistake! Thank you. |
| ||
To be honest, I can't work out what it is that you're trying to do in order to say what you're doing right or wrong altogether. As AndyGFX wrote, Resize returns the new array, so you have to assign it back but I'm not sure that achieves what you want.Print("Length before resize (2D): " + array2D[0].Length()) array2D[0] = array2D[0].Resize(5) Print("Length after resize (2D): " + array2D[0].Length()) You see, this code doesn't resize array2D. It resizes the first element of array2D. |
| ||
I realize this. I recreated a minimal version that demonstrated the problem I was having on a very small scale. As I indicated from my previous post, the issue is now solved. There were two problems: - I was allocating arrays wrongly. (Thanks muddy_shoes) - I was using Array.Resize() incorrectly. (Thanks AndyGFX) The original code which I wrote the night before is also embarrassingly unsound as it was a wee bit past my bed time. >.< Thank you. |