B4R Question Two Dimensional Arrays?

daveinhull

Active Member
Licensed User
Longtime User
Hi,

Is it possible to have two dimensional arrays in B4R?
I've search the forum, but nothing comes up for B4R and the IDE/Compiler says No!

Thanks
Dave
 

thetahsk

Active Member
Licensed User
Longtime User
You have to flatten the 2D matrix, create your usual 1D Array and use a mapping function to access the elements as a 2D array.
 
Upvote 0

emexes

Expert
Licensed User
a mapping function
or roll your own extra dimension, eg for a 12 x 16 array:
B4X:
Dim A(12 * 16)    'equivalent to Dim A(12, 16) ie both allocate 192 elements

A(5 * 16 + 8) = 42    'equivalent to A(5, 8) = 42

For I = 0 to 12 - 1
    For J = 0 to 16 - 1
        Log("A(" &  I & ", " & J & ") = " & A(I * 16 + J))    'equivalent to ... A(I, J)
    Next
Next
edit: I saw "mapping function" and thought this meant B4X Map container and massive performance hit, but in retrospect "I * 16 + J" could also be called a mapping function and perhaps my performance nerve fired unnecessarily ?
 
Last edited:
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
... my performance nerve fired unnecessarily ?

Your "friendly fire" with fluffy marshmallows has not harmed my bulletproof vest.

1594101824591.png
 
Upvote 0

daveinhull

Active Member
Licensed User
Longtime User
Thanks Folk,

I ended up doing it with a mapping function into a single dimension array.
Is there any particular reason why we can't have two or multiple dimensional arrays in B4R?

Thanks
Dave
 
Upvote 0
Top