B4J Question List to Array

Blueforcer

Active Member
Licensed User
Is there a simple way to copy the content of a List into a Array?

For now i do it like that. but because this happens every 80ms, i thought there might be a faster/simpler way.

B4X:
Dim bmproot As List

Dim bmp(bmproot.Size) As Int
For index=0 To bmproot.Size-1
    bmp(index)=bmproot.Get(index)
Next
 
Last edited:

PatrikCavina

Active Member
Licensed User
Using JavaObject you can use only 3 lines:
B4X:
Dim bmproot As List
Dim joList As JavaObject = bmproot
Dim bmp() As Object = joList.RunMethod("toArray", Null)
 

Erel

Administrator
Staff member
Licensed User
i thought there might be a faster/simpler way.
Your code is very simple. The question is whether it is fast enough.

Test it:
B4X:
    Dim bmproot As List
   bmproot.Initialize
   For i = 1 To 100000
       bmproot.Add(Rnd(1, 10000))
   Next
   
   
   Dim n As Long = DateTime.Now
   For Test = 1 To 10000
       Dim bmp(bmproot.Size) As Int
       For index=0 To bmproot.Size-1
           bmp(index)=bmproot.Get(index)
       Next
   Next
   Log((DateTime.Now - n) / 10000)
On my computer it takes 0.1ms to create the array and fill it. Tested with a list of 100k numbers.
This is of course fast enough.
 

Blueforcer

Active Member
Licensed User
Youre right. I should messure it before.
But because my List contains maximum 256 Items, the time will be here no longer worth mentioning.
Thnak you!
 
Top