B4J Question How to create multi-dimensional list...

Cableguy

Expert
Licensed User
Longtime User
Can you give an example?
Maybe you can achieve what you want using an array of lists or a set of lists inside a list, and declare them as type...
 
Last edited:
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Here is an example:
B4X:
Sub Test
    Dim lst As List
    lst.Initialize
    Dim n(2) As Int
    ' populate the list
    n(0) = 1
    n(1) = 2
    lst.Add( n )
    Dim n(2) As Int ' new declaration, see below
    n(0) = 3
    n(1) = 4
    lst.Add( n )
    '...
    ' retrieve list elements
    n = lst.Get(0)
    Log( n(0) & "," & n(1)) ' prints "1,2"
    n = lst.Get(1)
    Log( n(0) & "," & n(1)) ' prints "3,4"
    ' change list content indirectly
    n(0) = 5 ' this is still pointing to the object that was added to the list element lst.Get(1)
    n(1) = 6
    Dim m() As Int = lst.Get(1) ' create a new reference to n() from lst.Get(1)
    Log( m(0) & "," & m(1)) ' prints 5,6
End Sub

Unfortunately, there is no array notation like n( x, y, z ) and there is no easy way to add a dimension without losing the existing content (you can do it but you have to write code).

Note that n() is redeclared because when you add something to a list, you do not actually copy it to the list, you create a reference to it and add the reference to the list (think pointers if you are familiar with C).
If you only declare n() one time and add it to the list multiple times, you just add multiple references to the same object so that if you change that object later, the list content will change as well.
By redeclaring n(), you erase the 'n' reference to the original array and create a new instance of n pointing to a different area of memory.
When you use lst.Get(), you simply create a new reference to the original object

It took me a while to understand this but it actually makes sense and while less convenient if you are used to the n(x, y, z ) notation, it allows some neat tricks.

In my programs, I sometimes deal with it by storing CSV data in a list and using Regex.Split to retrieve it. It is not very efficient but for short lists it works surprisingly well.
I also use custom types that I add to a list when CSV does not work.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Of course you are correct. I lost track of the original question and got carried away with the example using lists. One problem with the multidimensional arrays is that they cannot be redimentioned.
 
Upvote 0

emexes

Expert
Licensed User
A multidimensional list sounds like a sparse array. This approach below might make your brain explode, but... have you considered using a Map, with the dimension indexes as a key?
B4X:
Dim M As Map
M.Initialize

For Each I1 As String In Array As String("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
    For Each I2 As String In Array As String("North", "South", "East", "Middle", "Gymnasium")
        For Each I3 As String In Array As String("1", "2", "3", "Hall", "5", "6")
            If Rnd(1, 100) <= 70 Then    'random 70% of rooms utilized
                Dim I123 As String = I1&"_"&I2&"_"&I3
                Dim NumStudents As Int = Rnd(5, 35)    'random data
                M.Put(I123, NumStudents)
            End If
        Next
    Next
Next

Log(M)

'there is a 30% chance this room won't have an entry, but let's give it a go anyway

Dim I123 As String = "Monday"&"_"&"North&"_"&5
Log(M.Get(I123))

Log(M.Get("Monday_North_5"))
 
Upvote 0

emexes

Expert
Licensed User
How to create and populate multi-dimensional list?
Can you give an example?
+1

If a multidimensional array is an array of an array, continued for n dimensions, then presumably a multidimensional list is a list of a list, continued for n dimensions.

But in a multidimensional array, you can easily traverse it in any of the dimensions.

In a multidimensional list, this is a little harder to do, unless you have indexes in each of the dimensions so that you can "line up" which elements are to be included in a one-dimensional traversal, from the other n-1 dimensions.

So I am with Cableguy: an example would be useful :)
 
Upvote 0
Top