How to access elements in a lists ?

BlitzMax Forums/BlitzMax Beginners Area/How to access elements in a lists ?

ninomojo(Posted 2006) [#1]
Hello,

Let's say I have this "Tdot" type of "dots, containing X and Y fields and I create a list of 100 dots like this:

local DotList:Tlist = New Tlist
For i = 0 to 99

DotList.AddLast( Tdot.Create() )

next


Now that my 100 dots are created, how can I access, say, the 28th dot in the list and change the value of its X or Y field?

Thank a lot for helping, I'm stuck :)

Nino


ninomojo(Posted 2006) [#2]
And by the way, is there any difference between creating a list using "MyVariable = New Tlist" and "MyVariable = Createlist()" ?

Thanks


Dreamora(Posted 2006) [#3]
no there is no difference in creating.

And if you plan to get a specific dot / element, don't use lists!
They aren't meant for indexed access.
For such things, better use an array on which you can directly access an element. (lists have the functionality as well but it will read the previous 27 objects to find the 28th)

when working with lists, you will normally use "for local dot:TDot = eachin dotlist"


Booticus(Posted 2006) [#4]
But to answer your original question:



The function called "comparison" shows that you can at least exit the for-next loop if your comparison operation is true. Like Dreamora states, its not the most "efficient" way of doing things, but its the way *I* do things and I find it doesn't impact performance for my purposes.


Dreamora(Posted 2006) [#5]
You could simply use dotlist.valueatindex(28) to get it, you don't need any super cryptical way of getting the element with ID 28 :-)

But as mentioned, it needs to visit the first 27 elements to find element 28 as a linked list does not know anything of "absolute position", only relative positions.


CS_TBL(Posted 2006) [#6]
Arrays of objects can be resized dynamically, they prolly have all the functionality you need.. (look up slices)


Perturbatio(Posted 2006) [#7]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1586


ninomojo(Posted 2006) [#8]
Thanks a lot guys, I get it. It seems that it's no good to use lists here indeed. Too bad because I was pretty relaxed at the idea of being able to add and remove elements while not having to worry about array dimensions.

Is there a way to re-dimension an array of objetcs without losing all the data inside those objects? I'm basically trying to make a 2D editor where you can build objects from many sprites. So basically I wanted an object to be a list of sprites instead of being an array of sprites.

As I'd really like to have virtually no limit to the amount of sprites used, a way to redimension this array would be welcome. I don't see how I can do it with slices...

Thanks a lot once again.


CS_TBL(Posted 2006) [#9]
yes, look up slices

local bla:int[10]

array of 10

bla=bla[..20]

array of 20, first 10 are still there!

'20' can also be a variable..


Perturbatio(Posted 2006) [#10]
Thanks a lot guys, I get it. It seems that it's no good to use lists here indeed. Too bad because I was pretty relaxed at the idea of being able to add and remove elements while not having to worry about array dimensions.

http://www.blitzbasic.com/codearcs/codearcs.php?code=1586


ninomojo(Posted 2006) [#11]
@ CS_TBL

Oooh thanks ! So you can actually slice BIGGER :) Neat.

@ Pertubatio
Very cool looking, let me check that out !

Thanks again, you're the dudes :)


H&K(Posted 2006) [#12]
I wouldnt give up on the list idea altogether ninomojo.

Lets say the reason you want to change the 28th dot is because you have compared current position with expected position of each dot, and the first one you want to change is dot 28. You have in fact already looped thought the first 27 dots to find that you dont have to move them.

I know this is an arbitary example, but if you are checking/updateing all the dots each cycle, then maybe a list is the way to go.
For Loop For each Dot
Check does This Dot need to move
If yes loop through list of dots until this dot, and move it
End each dot loop

for each dot in list
Check does this dot need to move
If yes move it
End each dot loop

Im not sudjesting that you are doing the first one, but you must in some way have desided to move the 28th dot, and so if it is because you are effectivly looping through them anyway, then the second method works