Android Code Snippet Convert map to array

MapToArray:
public Sub mapToArray(m As Map) As Object()
    Dim arr(m.Size * 2) As Object  'array size is double of the map because both keys and values are elements
    Dim i As Int=0
    Dim value As Object 
    For Each key As Object In m.Keys
           value=m.Get(key)
        LogColor($"key=${key}, value=${value}"$, Colors.blue)
        arr(i)=key
        i = i +1
        arr(i)=value
        i =i +1 
    Next
    
    Return arr 
End Sub

Example usage:
    Dim mapMonths As Map = CreateMap("January": "1", "February": "2")
    Dim a() As Object=mapToArray(mapMonths)
    For Each item As Object In a
        Log(item) 'print out each array element
    Next
 

toby

Well-Known Member
Licensed User
Longtime User
The resulting array is similar to the array used in
B4X:
httpJob.Download2(url, arr)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…