B4J Question Elements in multi-dimensional lists

B4JExplorer

Active Member
Licensed User
Longtime User
What's the easiest way, in B4J, to refer to an element in a nested list?

So specifically, if you had

List1.AddAll( Array As String( "one", "two", "four" ) )
List2.AddAll( Array As String( "seventeen", "thirteen", "five" ) )
List3.AddAll( Array As String( "hundred", "eight", "zero" ) )

Listparent.Add( List1 )
Listparent.Add( List2 )
Listparent.Add( List3 )


, and you wanted to obtain the "thirteen" at ( 1, 1 ),

is there a simple way to do that, with B4J syntax?

Also, if you wanted to replace the element at (1,1), what is the simplest way to to that?



Essentially, I've been just Dimming a temporary sublist on the fly, setting it to the second child list and changing it, and then replacing it back into the parent. It's more complicated than that, but something along those lines. But there's got to be a simpler way.
 
Last edited:

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I would do something like this:

B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

Dim list1 As List : list1.initialize
Dim list2 As List : list2.initialize
Dim list3 As List : list3.initialize

list1.AddAll( Array As String( "one", "two", "four" ) )
list2.AddAll( Array As String( "seventeen", "thirteen", "five" ) )
list3.AddAll( Array As String( "hundred", "eight", "zero" ) )

Dim parentlist As List : parentlist.initialize
parentlist.Add(list1)
parentlist.Add(list2)
parentlist.Add(list3)

Log(getMultiListValue(parentlist,1,1))
setMultilistValue(parentlist,1,1,"Two")
Log(getMultiListValue(parentlist,1,1)) 

End Sub

private Sub getMultiListValue(Parent As List, IndexP As Int, IndexC As Int) As String
Dim child As List = Parent.Get(IndexP)
Return child.Get(IndexC)
End Sub

private Sub setMultilistValue(parent As List, indexP As Int, IndexC As Int, NewValue As String)
Dim child As List = parent.Get(indexP)
child.Set(IndexC,NewValue)

End Sub
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
I would do something like this:

B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

Dim list1 As List : list1.initialize
Dim list2 As List : list2.initialize
Dim list3 As List : list3.initialize

list1.AddAll( Array As String( "one", "two", "four" ) )
list2.AddAll( Array As String( "seventeen", "thirteen", "five" ) )
list3.AddAll( Array As String( "hundred", "eight", "zero" ) )

Dim parentlist As List : parentlist.initialize
parentlist.Add(list1)
parentlist.Add(list2)
parentlist.Add(list3)

Log(getMultiListValue(parentlist,1,1))
setMultilistValue(parentlist,1,1,"Two")
Log(getMultiListValue(parentlist,1,1)) 

End Sub

private Sub getMultiListValue(Parent As List, IndexP As Int, IndexC As Int) As String
Dim child As List = Parent.Get(IndexP)
Return child.Get(IndexC)
End Sub

private Sub setMultilistValue(parent As List, indexP As Int, IndexC As Int, NewValue As String)
Dim child As List = parent.Get(indexP)
child.Set(IndexC,NewValue)

End Sub
Well, that's definitely simpler. But I was hoping there was some syntax already in the language, that could deal with elements at nested levels directly.

Doesn't look like it. Ok, thanks Enrique, I'll give this a try.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
You are welcome.

AND there is an even more "simpler" but not so practical way. if the number of list is known you can create something like this:


B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

Dim list1 As List : list1.initialize
Dim list2 As List : list2.initialize
Dim list3 As List : list3.initialize

list1.AddAll( Array As String( "one", "two", "four" ) )
list2.AddAll( Array As String( "seventeen", "thirteen", "five" ) )
list3.AddAll( Array As String( "hundred", "eight", "zero" ) )

Dim arrlist() As List = Array As List(list1,list2,list3)
Log(arrlist(1).Get(1))
arrlist(1).Set(1,"Two")
Log(arrlist(1).Get(1)) 
End Sub

as you can see this is a very good shortcut: arrlist(1).Get(1))
 
Upvote 0

B4JExplorer

Active Member
Licensed User
Longtime User
You are welcome.

AND there is an even more "simpler" but not so practical way. if the number of list is known you can create something like this:


B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

Dim list1 As List : list1.initialize
Dim list2 As List : list2.initialize
Dim list3 As List : list3.initialize

list1.AddAll( Array As String( "one", "two", "four" ) )
list2.AddAll( Array As String( "seventeen", "thirteen", "five" ) )
list3.AddAll( Array As String( "hundred", "eight", "zero" ) )

Dim arrlist() As List = Array As List(list1,list2,list3)
Log(arrlist(1).Get(1))
arrlist(1).Set(1,"Two")
Log(arrlist(1).Get(1)) 
End Sub

as you can see this is a very good shortcut: arrlist(1).Get(1))
This is closer to what I was looking for, and I think would work in this situation.

Good stuff, thanks E.G.
 
Upvote 0
Top