TList

BlitzMax Forums/BlitzMax Beginners Area/TList

po(Posted 2007) [#1]
I know you can have a list of objects, but can you also have a list of values? If so, how?


grable(Posted 2007) [#2]
If you mean values as in Int/Float etc,
you need to use either arrays or a type that holds the value in a field.


bradford6(Posted 2007) [#3]
do you mean arrays?

something like this:
Local pencil_Boxes:Int[10]
pencil_boxes[0] = 4    ' this box has 4 pencils
pencil_boxes[1] = 6    ' and this box has 6 pencils
Print pencil_boxes[0]
Print pencil_boxes[1]



Arrays in BlitzMax are very versatile. (sometimes even moreso than lists) An Array can hold a number (i.e an int, a float, Double, etc) or can hold a String ( "foo" )

you can resize them on the fly by using slices:
Local foo:String[]

foo = foo[..2]		' a slice
foo[0] = "Hello"
foo[1]= "GoodBye"

Print "foo has " +Len(foo)+ " Elements"
Print foo[0] 
Print foo[1]



SculptureOfSoul(Posted 2007) [#4]
Note that resizing an array is considerably slower than adding an element to the end of a list, while accessing an element in an array is considerably faster.

If you want a TList of values, you have to wrap the values in a type, as grable mentioned. For example:


type IntWrapper

field Value:int

Endtype



Then you store instances of IntWrapper in the list.


po(Posted 2007) [#5]
Right, ok. Thanks all.


bradford6(Posted 2007) [#6]
http://www.blitzbasic.com/Community/posts.php?topic=67497