Android Question Array of array (and its initialization)

StefanoAccorsi

Member
Licensed User
Longtime User
Hi there,
I searched in Google and here in the forum but can't find any example or tutorial on declaring an array of array and on how to initialize it.

I mean something like

Code: Copy
((Int, Int), (Int, Int), (Int, Int), ...)

I started from the idea of an array of custom type:

Code: Copy
Type Coord(x as int, y as int)
Dim arr() as Coord

This could be ok, but I need to manually initialize it with something like (in pseudo-Java code):

Code: Copy
arr = Array of Coord(new Coord(1,4), new Coord(5,6), ...)

Is it possible to write something like this in B4A?

Otherwise, an array of array of two int could be ok, but I don't know how to initialize it, and even how to correctly declare it. Possibly something like:

Code: Copy
Dim arr(Array(2) as Int) as ???

I wrote the code up there since it didn't give me syntax error!

And then, how to initialize it with something like:

Code: Copy
arr = Array as ???((1,4),(5,6),...)

Thank you!
 
You can create a multi-dimension array:
Code: Copy
Dim a(10, 20) As Int
Dim b(10, 20, 30, 40) As Double)
However you cannot use the Array keyword with these arrays.

If you want to create an array of arrays then you should use a list instead.
A list can hold any type of objects including arrays.

Code: Copy
Dim l1 As List
l1 = Array(Array As Int(1, 2), Array As Int (3, 4))
For Each a() As Int In l1
   Log(a(0) & ", " & a(1))
Next

Don't overuse arrays. It will be easier to work with lists, maps and custom types.
 
Upvote 0
I'm envious for that Like from @ArminKH.
My solution is very similar to the Erel's one (also, it has the advantages of Maps).

upload_2016-2-7_10-27-29.jpeg



:p:p:p
 
Upvote 0
Back
Top