Android Question Getting the length of a multi-dimensional array

wonder

Expert
Licensed User
Longtime User
So I have the following (x, y) array:
B4X:
Dim MyArray(10, number_of_elements) as Int
Where the number of elements is defined by the user.

When using the MyArray.Lenght property, I get the number 10.
How to get the lenght of the array's Y dimension through .lenght?
 

wonder

Expert
Licensed User
Longtime User
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Wonder,
there is no magic within this code. It uses the possibility of java called reflection which let you examine java classes at runtime, but this you already knows. And it is not my code, "big brother" google found it for me....but thanks for the honor

stefan
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User

B4X:
Sub GetArrayInfo(oArray As Object) As Int()

Dim i As Int
Dim r As Reflector
Dim dims() As Int

r.Target = oArray
 dims = r.TargetRank
 Dim arrInt(dims.Length + 1) As Int
arrInt(0) = dims.Length

For i = 0 To dims.Length -1
arrInt(i + 1) = dims(i)
Next

Return arrInt

End Sub

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Replying to conversation from Exemes regarding this:

Yes, still working fine, using Reflection library 2.4:

B4X:
Sub GetArrayInfo(oArray As Object) As Int()

    Dim i As Int
    Dim r As Reflector
    Dim dims() As Int

    r.Target = oArray
    dims = r.TargetRank
    Dim arrInt(dims.Length + 1) As Int
    arrInt(0) = dims.Length

    For i = 0 To dims.Length -1
        arrInt(i + 1) = dims(i)
    Next

    Return arrInt

End Sub

Sub TestArrayDims
    
    Dim i As Int
    Dim arr2D(4,2) As String
    Dim arrInt() As Int = GetArrayInfo(arr2D)
    
    For i = 0 To arrInt.Length - 1
        Log("arrInt(" & i & "): " & arrInt(i))
    Next

End Sub

RBS
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…