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

Discussion in 'B4J Questions' started by rbghongade, Jul 1, 2019.

  1. rbghongade

    rbghongade Active Member Licensed User

    Dear Friends,
    How to create and populate multi-dimensional list?
  2. Cableguy

    Cableguy Expert Licensed 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: Jul 2, 2019
    emexes likes this.
  3. Didier9

    Didier9 Active Member Licensed User

    Here is an example:
    Sub Test
    Dim lst As List
    Dim n(2As Int
    ' populate the list
        n(0) = 1
    1) = 2
        lst.Add( n )
    Dim n(2As Int ' new declaration, see below
        n(0) = 3
    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.
    rbghongade likes this.
  4. LucaMs

    LucaMs Expert Licensed User

    Try Dim n(1,2,3,4,5) As Int

    Dim a,b,c,d,e As Int
    a = 
    b = 
    c = 
    d = 
    e = 
    Dim n(a,b,c,d,e) As Int
    if you meant this.
  5. Didier9

    Didier9 Active Member Licensed 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.
  6. emexes

    emexes Well-Known Member 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?
    Dim M As Map

    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(1100) <= 70 Then    'random 70% of rooms utilized
                    Dim I123 As String = I1&"_"&I2&"_"&I3
    Dim NumStudents As Int = Rnd(535)    'random data
                    M.Put(I123, NumStudents)
    End If


    '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

    rbghongade likes this.
  7. emexes

    emexes Well-Known Member Licensed User


    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 :)
  8. LucaMs

    LucaMs Expert Licensed User

    I agree; sometimes (often?) you should use custom types or classes.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice