Android Code Snippet Array tools: getDimSize and getSize

Regarding the matter discussed on this thread, based on @eurojam's code (thanks!), I came up with the functions below:

getDimSize
B4X:
'Returns the array's specified dimension size.
'Dimension zero will return the array's number of dimensions and respective sizes.
'getDimSize by 'wonder', based on the solution provided by 'eurojam' at B4X forums
'https://www.b4x.com/android/forum/threads/getting-the-length-of-a-multi-dimensional-array.54614/#post-342944
Sub getDimSize(MyArray As Object, Dimension As Int) As String
    Dim r As Reflector
    Dim temp As String
    Dim dims() As Int
    r.Target = MyArray
    dims = r.TargetRank
    If Dimension <= 0 OR Dimension > dims.Length Then
        For idx = 0 To dims.Length - 1       
            temp = temp & dims(idx)
            If idx < dims.Length - 1 Then temp = temp & ", "       
        Next
        Return dims.Length & "D Array: {" & temp & "}"
    Else
        Return dims(Dimension - 1)
    End If
End Sub


getSize
B4X:
'Returns the total size of the specified array.
'getSize by 'wonder', based on the solution provided by 'eurojam' at B4X forums.
'https://www.b4x.com/android/forum/threads/getting-the-length-of-a-multi-dimensional-array.54614/#post-342944
Sub getSize(MyArray As Object) As String
    Dim r As Reflector   
    Dim dims() As Int
    Dim calc = 1 As Int
    r.Target = MyArray
    dims = r.TargetRank
    For idx = 0 To dims.Length - 1       
        calc = calc * dims(idx)
    Next
    Return calc
End Sub


Examples:
B4X:
Dim test_array(3, 5, 9) as Int
getDimSize(test_array, 0)

Output: "3D Array: {3, 5, 9}"

B4X:
Dim test_array(3, 5, 9) as Int
getDimSize(test_array, 2)

Output: 5

B4X:
Dim test_array(3, 5, 9) as Int
getSize(test_array)

Output: 135
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Why a string as output? A List should be better.
Because strings are sexy! ;)
649cefdeb71fa7f676dcd6ef1afb77c4.jpg

Feel free to fit the code to your own needs, Luca. :)
 
Last edited:
Top