Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Find element in Array need explaination

Author
Message
hoyoyo80
8
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 9th Sep 2017 06:27
Hi all,

im putting value 1,2,3 inside an array then i try to use array.find() function to fetch the element back, but find(0) to find(2) return the value -1,0,1. Can someone explain to me the concept? Thanks
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 9th Sep 2017 11:03
Arrays are indexed from zero. So your array reads:

index 0: value 1
index 1: value 2
index 2: value 3

When using .find (which can only be used on a sorted in ascending order array) on your array, -1 means not found. Any other result is the index of where the value is found. So in your example:

.find(0): value not found -1
.find(1): value found at index 0
.find(2): value found at index 1
hoyoyo80
8
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 10th Sep 2017 02:14
myArray as integer[5] = [3,4,1,5,2,6]
myArray.sort() // array is now [1,2,3,4,5,6]
index = myArray.find(4) // will return "3" as array indexes start at 0

This is from the help file.
So mean that find() actually return the index base on element pass in the find function?

Thanks

Login to post a reply

Server time is: 2024-11-22 20:23:49
Your offset time is: 2024-11-22 20:23:49