Return object from a List

Monkey Forums/Monkey Programming/Return object from a List

FelipeA(Posted 2012) [#1]
Hello, I just started with monkey yesturday, so I still don't know much of it.

I was wondering if there is a way of getting and object of a list on a especific position, for example I have a list of images and I want to return only the one at number 2.

I hope someone can help me with this.

Thanks!


therevills(Posted 2012) [#2]
Because list are linked list, the objects are joined by nodes so there isnt an easy command to get the object at a set point. But you can do stuff like this:

Strict

Import mojo

Global list:List<Int> = New List<Int>

Function Main:Int()
	list.AddLast(10)
	list.AddLast(20)
	list.AddLast(30)
	
	Print "Everything in the list:"
	For Local i:Int = Eachin list
		Print i
	Next
	
	Print ""
	
	Print "Whats the second on in the list?"
	Print GetObjectAt(1) ' 0 based!

	Print ""
	Print "Let's convert the list into an array..."
	Local arr:=list.ToArray()
	Print arr[1]

	Return 0
End

Function GetObjectAt:Int(index:Int)
	Local counter:Int = 0
	
	For Local i:Int = Eachin list
		If counter = index
			Return i
		End
		counter+=1
	Next
	Return 0
End


Another option is to try Diddy's ArrayList where it is indexed ;)


Samah(Posted 2012) [#3]
Or you can use Stack like a List.


FelipeA(Posted 2012) [#4]
Thanks for the answer.
I'll go with Samah suggestion, as it looks a lot simple and similar to as3 arrays or vectors.