B4J Question List to Array

Discussion in 'B4J Questions' started by Blueforcer, Apr 16, 2019.

  1. Blueforcer

    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.

    Code:
    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: Apr 16, 2019
  2. PatrikCavina

    PatrikCavina Active Member Licensed User

    Using JavaObject you can use only 3 lines:
    Code:
    Dim bmproot As List
    Dim joList As JavaObject = bmproot
    Dim bmp() As Object = joList.RunMethod("toArray"Null)
     
    Blueforcer likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    What do you need it for?
     
  4. Blueforcer

    Blueforcer Active Member Licensed User

  5. Erel

    Erel Administrator Staff Member Licensed User

    Your code is very simple. The question is whether it is fast enough.

    Test it:
    Code:
    Dim bmproot As List
       bmproot.Initialize
       
    For i = 1 To 100000
           bmproot.Add(
    Rnd(110000))
       
    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 likes this.
  6. Blueforcer

    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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice