Filling Arrays - The Easy Way?

sterlingy

Active Member
Licensed User
Longtime User
I know that a single dimensional array Grid() can be populated with Grid = Array As (1,2,5,3,8,4)

I need to fill a two dimensional Array Grid(,)

Is there a similar quick method for multi-dimensional arrays?

-Sterling
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can create one with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim grid(,) As Int = Create2DArray(3, Array As Int(1, 2, 3, 4, 5, 6))
   'grid =   1, 2, 3
   '      4, 5, 6
End Sub

Sub Create2DArray(RowLength As Int, Arr() As Int) As Int(,)
   Dim rows As Int = Arr.Length / RowLength
   Dim td(rows, RowLength) As Int
   For r = 0 To rows - 1
      For c = 0 To RowLength - 1
         td(r, c) = Arr(r * RowLength + c)
      Next
   Next
   Return td
End Sub
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
That works for me!

Thanks Erel.

-Sterling
 
Upvote 0
Top