Array question about [,] vs [][]
BlitzMax Forums/BlitzMax Programming/Array question about [,] vs [][]
| ||
Hi, I'm wondering what the technical difference between an array using [,] and an array using [][] is. As far as I can tell the big difference is that the [][] type is alot more complicated to setup, but I'm sure I'm missing the big picture. Does anyone know? And why would you use [][] instead of [,]? A comparison: Strict Local a:Int[5,5] Local b:Int[][] = [New Int[5],New Int[5],New Int[5],New Int[5],New Int[5]] For Local i:Int = 0 To 4 For Local j:Int = 0 To 4 a[i,j] = i + j b[i][j] = i + j Next Next For Local i:Int = 0 To 4 For Local j:Int = 0 To 4 Print a[i,j]+" vs. "+b[i][j] Next Next |
| ||
Well, you could more efficiently use memory with [][] I think, since you could nullify specific areas of the array and thus let the data be picked up by the garbage collector, compared to it always being allocated. Edit: Another thing is that each sub-array can be variable length with [][], whereas [,] is basically a square array. Aside from that, I think it's personal preference. |
| ||
[,] means each second array [x,this] are the same size but [][] the second arrays [x][this] can be different sizes. ie [,] is a 2d array, but [][] is an array of arrays, a 1d array of type array So if you want (for example) a map then [,] If on the other hand you want a map with what units are in each square [,][] In your example Array:Int [5,5] is a 5 by 5 array of type Int While Array:Int[][] = [New Int[5],New Int[5],New Int[5],New Int[5],New Int[5]] Is a 5 segment array of type 5 segmant int array |
| ||
[,] mens you are sing a 2 dimensional array (matrix). [][] means you are using an array of arrays. |
| ||
There is one more interesting point in using [][] above [,] [][] can be sliced! (as well as have subarrays of differet sizes), while [,] can't be sliced (and is always a "rectangle area of variables") |
| ||
isn't [][] called a "jagged" array. |
| ||
don't know ... normally it is called "array of array" or dynamic array of multiple dimensions and the like. |
| ||
A Jagged array is exactly what it looks like on an old "one font" printer/screen display. So I can Imagine that being the name. But Its just as likly its a coloqualism. |
| ||
Here is an example of each type of array. In the first example we have a Matrix [x,y] type array. these arrays are like excel spreadsheets or a piece of graphpaper. these cannot be sliced. they have an advantage in that they are easier to set up and more intuitive (for me at least) to use: [X,Y] 0,1,2,3,4 0,1,2,3,4 0,1,2,3,4 0,1,2,3,4 This is an Array of Arrays [X][Y]. these offer much more flexibility. (can be sliced, can be resized) but require more setup and are less intuitive. Check out the output for Element Map[3] after the resize: [X] [....Y.....] [0] [0,1,2,3,4] [1] [0,1,2,3,4] [2] [0,1,2,3,4] [3] [0,1,2,3,4,5,6,7,8,9...] [4] [0,1,2,3,4] So the Biggest advantage of 'Array of Arrays' is that they can be resized, sliced, diced etc. you can start off with an emty array[][] and resize it from within your program. |
| ||
I'll paste a better example in awhile |
| ||
quick question... as this just jumped out at me... i dunno if this holds true in max, but isn't one an example of late binding, and the other early binding... what i mean is , if something is declared as x=new SomethingOrTheOther, isn't it created immediately, where as the other methods creates a placeholder in memory for the type? or am i all screwed up on this :) --Mike |
| ||
or am i all screwed up on this :) There are so many ways to respond to that. |
| ||
well... don't be shy... lets hear it some of those ways :) (all within the scope of the discussion, of course... i mean, after all... this is a serious programming forum, and not the open discussion cafeteria below) --Mike |
| ||
'new' creates an instance of a type. a type declaration is just a blueprint of an object to be. Type Animal field Name:String End Type does not do anything until we create an INSTANCE Dog:animal = new animal Dog.name = "Fido" as far as 'late binding' vs 'early binding' I think that just has to do with when you instantiate your objects. i.e on the fly when needed or at compile time. not really 100% sure though |
| ||
Hi, Thanks :) I think I'll just stick with regular multi dimensional arrays, and type arrays. Arrays of arrays seem a bit too much for me. |
| ||
Fred we've all probably made arrays of arrays sound a lot more complicated than they are. But to make it sound simple. An array of Strings, is an array of arrays. (Because the strings can be of different sizes) In the old days of basic you would use a multi array with [numberofstrings,MaxStringSize]. Which was a total waste of space. Now we dont, as the language now treats the string as a list of charecters The basic thing is If you need to store an unspecific number of things a specific numer of times you should use an array of arrays. (Or an array of TLists I suppose) I know that the memory problem is secondary nowadays. But its still good programming practice |
| ||
fredborg, it is really not complicated at all. check this out: Local mystringarray:String[10] mystringarray[0] = "hello" mystringarray[1] = "World" Print mystringarray[0] + " " + mystringarray[1] For Local letter:Int = 0 To Len(mystringarray[0])-1 Print "MyStringArray[0]["+letter+"]="+Chr(mystringarray[0][letter]) Next |
| ||
In BMX an array is maintained by keeping track of a pointer to the first element in a continguous block of data. The values of that data is the data in the array. When you do [,] the value is a continguous array of all the data in the entire rectangular array, and some simple math is used to convert the rectangular coordinates to a linear position. But when you use [][], the value of the first array contains pointers to more arrays, which hold your actual data.A one dimensional array: array:Int[3] $00 $01 <- location of array in memory, pointing to $01, the location of the first element $01 123 <- first element of array, array[0] $02 456 <- second element of array, array[0] $03 789 <- third element, array[1] Then a two dimensional array: array:Int[3,3] $00 $01 <- location of array, points to first element at $01 $01 123 <- first element, array[0, 0] $02 456 <- second element, array[0, 1] $03 789 <- array[0, 2] $04 987 <- array[1, 0] $05 654 <- array[1, 1] $06 321 <- array[1, 2] $07 65 <- array[2, 0] $08 66 <- array[2, 1] $09 67 <- array[2, 2] And finally an array of arrays: array:int[3][3] $00 $01 <- location of array, points to $01 $01 $04 <- array[0], which points to $04, the location of the values $02 $07 <- array[1], which points to $07 $03 $0A <- array[2], which points to $0A $04 123 <- array[0][0] $05 456 <- array[0][1] $06 789 <- array[0][2] $07 987 <- array[1][0] $08 654 <- array[1][1] $09 321 <- array[1][2] $0A 65 <- array[2][0] $0B 66 <- array[2][1] $0C 67 <- array[2][2] It helps to think about an array as an object, just like any other object like a TList or TImage or whatever. So you can have an array of arrays just like you can have an array of any object like an array of TImage's. Hope it helps. I was confused at first with the difference, but I read up on it and figured it out. |