StefanoAccorsi Member Licensed User Longtime User Feb 7, 2016 #1 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!
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!
LucaMs Expert Licensed User Longtime User Feb 7, 2016 #2 Use a Map. Code: Copy Dim mapCoord As Map : mapCoord.Initialize Dim Coord1(1, 4) As Int Dim Coord2(5, 6) As Int '... mapCoord.Put(1, Coord1) mapCoord.Put(2, Coord2) Upvote 0
Use a Map. Code: Copy Dim mapCoord As Map : mapCoord.Initialize Dim Coord1(1, 4) As Int Dim Coord2(5, 6) As Int '... mapCoord.Put(1, Coord1) mapCoord.Put(2, Coord2)
Erel B4X founder Staff member Licensed User Longtime User Feb 7, 2016 #3 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
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.
LucaMs Expert Licensed User Longtime User Feb 7, 2016 #4 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). 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).
Erel B4X founder Staff member Licensed User Longtime User Feb 7, 2016 #5 You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6)) Upvote 0
You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6))
LucaMs Expert Licensed User Longtime User Feb 7, 2016 #6 Erel said: You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6)) Click to expand... To do it, I should have more than 2 bytes of brain memory: that "new" way to initialize a map did not remain in memory Thank you, Erel Upvote 0
Erel said: You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6)) Click to expand... To do it, I should have more than 2 bytes of brain memory: that "new" way to initialize a map did not remain in memory Thank you, Erel
LucaMs Expert Licensed User Longtime User Feb 7, 2016 #7 https://www.b4x.com/android/forum/threads/addtomap.43937/#post-267332 Upvote 0
StefanoAccorsi Member Licensed User Longtime User Feb 7, 2016 #8 Erel said: You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6)) Click to expand... Damn, @Erel is frightful!! Thank you guys. Upvote 0
Erel said: You would have received a "like" if you made your code more elegant Code: Copy Dim MapCoord As Map = CreateMap (1: Array As Int(1, 4), 2: Array As Int(5, 6)) Click to expand... Damn, @Erel is frightful!! Thank you guys.