Flash / Flex / ActionScript/Array/search
Версия от 09:19, 26 мая 2010; (обсуждение)
A generalized array-searching function
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var soundtracks:Array = ["e", "h","p", "a", "c"];
trace(searchArray(soundtracks,"h"));
}
function searchArray (theArray:Array, searchElement:Object):int {
for (var i:int = 0; i < theArray.length; i++) {
if (theArray[i] == searchElement) {
return i;
}
}
return -1;
}
}
}
Search array elements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var soundtracks:Array = ["e", "h","p", "a", "c"];
for (var i:int = 0; i < soundtracks.length; i++) {
trace("Now examining element: " + i);
if (soundtracks[i] == "h") {
trace("The location of "h" is index: " + i);
break;
}
}
}
}
}
Now examining element: 0
Now examining element: 1
The location of "h" is index: 1