I am attempting to do a 2 dimensional label array (think of a grid of labels). But it doesnt work.
Here is my feeble attempt:
B4X:
'2 Dimensional Array
Sub CollectNodes2D(Tags() As String) As Label()
Dim TagsIndex As Int
Dim lblsList(Tags.length) As List
Dim MaxListSize As Int
For TagsIndex = 0 To Tags.Length-1
lblsList(TagsIndex).Initialize
For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1
Dim n As Node = OutputWindow.RootPane.GetNode(I)
If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n)
Next
If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it.
Next
Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array
For TagsIndex = 0 To Tags.Length - 1
For i = 0 To lbls.Length - 1
Dim lbl As Label = lblsList(TagsIndex).Get(i)
lbls(TagsIndex, i) = lbl
Next
Next
Return lbls
End Sub
But it gets an error:
main.java:402: error: incompatible types: LabelWrapper[][] cannot be converted to LabelWrapper[]
if (true) return (anywheresoftware.b4j.objects.LabelWrapper[])(_lbls);
Not sure how to do that. But after a little bit of thinking, I came to this and it worked finally:
B4X:
Sub CollectNodes2D(Tags() As String) As Label(,)
Dim TagsIndex As Int
Dim lblsList(Tags.length) As List
Dim MaxListSize As Int
Dim ListSize(Tags.Length) As Int
For TagsIndex = 0 To Tags.Length-1
lblsList(TagsIndex).Initialize
For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1
Dim n As Node = OutputWindow.RootPane.GetNode(I)
If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n)
Next
ListSize(TagsIndex) = lblsList(TagsIndex).Size 'Grab our list size for this particular Tag.
If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it.
Next
Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array
For TagsIndex = 0 To Tags.Length-1
For i = 0 To ListSize(TagsIndex) - 1
Dim lbl As Label = lblsList(TagsIndex).Get(i)
lbls(TagsIndex, i) = lbl
Next
Next
Return lbls
End Sub
It may not be the best or most efficient piece of code out there, many ways to skin the cat, but it works for what I needed it for. I figured I would post it in case someone else wants to use it.
The reason I needed to do that, is so I could do this: