Am I crazy? Didn't this used to work? (list to array)

Started by GW, July 15, 2017, 06:19:45

Previous topic - Next topic

GW

Converting a list to an array.  Am I having a senior moment?
Code (blitzmax) Select


SuperStrict
Framework brl.basic


Global sList:TList = CreateList()


'// Fill a list with strings
For Local i% = 0 Until 200
sList.Addlast( String(Rand(9999)) )
Next


'// Create an array from the list
Global sArr$[] = String[]( sList.toArray() )


'// string array is null!??
DebugStop

degac

No, your are not alone :)

Code (blitzmax) Select
SuperStrict
Framework brl.basic


Global sList:TList = CreateList()


'// Fill a list with strings
For Local i% = 0 Until 200
        sList.Addlast( String(Rand(9999)) )
Next


'// Create an array from the list
Global oArr:Object[]=Object[]( sList.toArray() )
Global sArr$[]
sArr=String[](ListToArray(sList))

'Arr=ListToArray(sList)


If sArr=Null
Print "sArr is null"

End If

'object convers
If oArr=Null
Print "oArr is null"

End If


It seems ListToArray() (or method) cast only to an 'OBJECT ARRAY'
Then you need to re-cast it manually (or via a second loop to create your string array)
If there's a problem, there's at least one solution.
www.blitzmax.org

Henri

Hi,

I find that the easiest way is to create your own TList:


Strict


'Testing
Local list:MyList = New MyList

For Local i:Int = 0 To 10
list.addlast( String(i) )
Next

Local ar:String[] = list.ToStringArray()

For Local ii:Int = 0 Until ar.length
Print ar[ii]
Next


Type MyList Extends TList

Rem
bbdoc: convert a list to an string array
returns: An array of strings
End Rem
Method ToStringArray:String[]()
Local arr:String[Count()],i
Local link:TLink=_head._succ
While link<>_head
arr[i]=String(link._value)
link=link._succ
i:+1
Wend
Return arr
End Method
EndType



-Henri
- Got 01100011 problems, but the bit ain't 00000001

Brucey

This is how you might do it with the new NG collections module...
Code (blitzmax) Select

SuperStrict
Framework brl.basic
Import brl.collections

Global sList:TArrayList<String> = New TArrayList<String>

'// Fill a list with strings
For Local i% = 0 Until 200
        sList.Add( String(Rand(9999)) )
Next

'// Create an array from the list
Global sArr$[] = sList.toArray()

'// string array is null!??
DebugStop

Note that sList is a list that only stores Strings (as specified in the variable declaration). When you call ToArray(), you don't need to cast, as BlitzMax will only return a String array from this list.

Henri

- Got 01100011 problems, but the bit ain't 00000001

GW

hmm. I was sure this used to work.  But I now understand some of my confusion.
List casting works for types but not for strings.
Code (blitzmax) Select
SuperStrict

Framework brl.basic


Type tTestType
   Field a#=1
End Type


Global List:TList =CreateList()
Global Array:tTestType[]


For Local i% = 0 Until 200
   list.addlast( New tTestType)
Next


Array = tTestType[]( list.ToArray() )
DebugStop
Print "Done: It works!"

Thanks for the assist guys!


Brucey: I would love to use NG for my current project, but I have the all too common problem of the GC blowing up when I allocate many objects.
About half of my projects are forced to stay with classic bmx because of this. But i would really love to go with NG full time.

markcwm

QuoteHmm...where is the TArrayList defined ?
You need to read more carefully Henri. :P
Import brl.collections
So it's a BRL module and as Brucey stated it's new for NG (since July 13).
https://github.com/bmx-ng/brl.mod/tree/master/collections.mod

Brucey

Quote from: GW on July 15, 2017, 19:58:51
Brucey: I would love to use NG for my current project, but I have the all too common problem of the GC blowing up when I allocate many objects.
About half of my projects are forced to stay with classic bmx because of this. But i would really love to go with NG full time.

I've never had this issue, although I understand the reasons for it. I would suggest you may have a problem with the design of your program, in that there may be ways to perform the same function without creating so many object instances.
Would you happen to have any examples of a smallish program that I may look at please, as I might be able to provide you with improvements that could get around the issue?

GW

I'll try to come up with an example.  In most situations i'm loading 500k objects from a file or sql db.