How to access elements in a lists ?

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

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

And by the way, is there any difference between creating a list using "MyVariable = New Tlist" and "MyVariable = Createlist()" ?

Thanks

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"

But to answer your original question:

SuperStrict
graphics 1024,768,32


Global DotList:TList = CreateList ()

For Local a:Int = 0 To 30
	Local theTdot:TDot = New TDot
	theTdot.Init(Rand(0,1024),Rand(0,768),a,Rand(0,5))
	ListAddLast DotList,theTdot
Next

Type TDot
	Field ID:Int
	Field x:Int
	Field y:Int
     Field myRadius:Int
     
	Method Init(theX:Int,theY:Int,theID:Int,theRadius:Int)
		x=theX
		y=theY
		myRadius=theRadius
		ID=theID
	End Method
	
	Method drawDot()
		DrawOval x,y,myRadius,myRadius
	End Method
	
	Method getID:Int()
		Return ID
	End Method             
	
End Type

While not KeyHit(KEY_ESCAPE) 
	Cls()
	drawDots
	Flip
Wend
          
Function drawDots()
	For Local theDot:TDot = EachIn DotList
          ' Here's where you do the comparison to find yours
		If theDot.ID=28
			SetColor(Rand(0,255),Rand(0,255),Rand(0,255))
			theDot.drawDot				
		Else
			SetColor 255,255,255
			theDot.drawDot
		EndIf
	Next
End Function

Function comparison()
	For Local theDot:TDot = EachIn DotList
		If theDot.ID=28
          	' Do your stuff
          	' exit will (theoretically) break us out of the for next loop?
          	Exit
		EndIf
	Next
End Function



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.

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.

Arrays of objects can be resized dynamically, they prolly have all the functionality you need.. (look up slices)

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

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.

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..

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

@ 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 :)

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