Android Code Snippet Array tools: getDimSize and getSize

Discussion in 'Code Snippets' started by wonder, Jun 8, 2015.

  1. wonder

    wonder Expert Licensed User

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

    getDimSize
    Code:
    '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
    'http://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
    Code:
    'Returns the total size of the specified array.
    'getSize by 'wonder', based on the solution provided by 'eurojam' at B4X forums.
    'http://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:
    Code:
    Dim test_array(359as Int
    getDimSize(test_array, 
    0)

    Output: 
    "3D Array: {3, 5, 9}"
    Code:
    Dim test_array(359as Int
    getDimSize(test_array, 
    2)

    Output: 
    5
    Code:
    Dim test_array(359as Int
    getSize(test_array)

    Output: 
    135
     
    Last edited: Jun 8, 2015
    LucaMs likes this.
  2. LucaMs

    LucaMs Expert Licensed User

    Why a string as output? A List should be better.
     
    wonder likes this.
  3. wonder

    wonder Expert Licensed User

    Because strings are sexy! ;)
    [​IMG]

    Feel free to fit the code to your own needs, Luca. :)
     
    Last edited: Jun 11, 2015
    MarcoRome and LucaMs like this.
  4. LucaMs

    LucaMs Expert Licensed User

    Very nice eyes :D
     
    Cableguy and wonder like this.
  5. Cableguy

    Cableguy Expert Licensed User

    She's wearing sunglasses!!
     
    LucaMs and wonder like this.
  6. LucaMs

    LucaMs Expert Licensed User


    ...there... but every time she comes to my house, she does not wear glasses.
     
    wonder likes this.
Loading...