Home  >  Article  >  php教程  >  Example introduction to the role of IndexOf of Array in Flex

Example introduction to the role of IndexOf of Array in Flex

高洛峰
高洛峰Original
2016-12-27 17:09:391472browse

The role of IndexOf of Array in Flex

1. Description

indexOf is used to search from small to large in the index. If it can be found, it will return the index value. If it cannot be found, it will return - 1;

2. Example

(1) Design source code

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
width="100%" height="100%" creationComplete="creationHandler(event)"> 
<s:layout> 
<s:BasicLayout/> 
</s:layout> 
<fx:Script> 
<![CDATA[ 
import mx.events.FlexEvent; 

/** 
* 初始化函数 
* indexOf用于在索引中从小到大查找 
* 如果查得到就返回索引值,查不到就返回-1 
*/ 
protected function creationHandler(event:FlexEvent):void 
{ 
var array:Array = ["桃树","梨树","松树","樟树"]; 
if(array.indexOf("梨树") != -1) 
{ 
trace("按索引从小到大查:"+array.indexOf("梨树")); 
} 
if(array.indexOf("樟树",3) != -1) 
{ 
trace("从第三个元素开始,按索引从小到大查:"+array.indexOf("樟树",3)); 
} 
} 

]]> 
</fx:Script> 
<fx:Declarations> 
<!-- 将非可视元素(例如服务、值对象)放在此处 --> 
</fx:Declarations> 
</s:Application>

(2) Running result


Press Search the index from small to large: 1
Start from the third element, search by index from small to large: 3

For more examples of the role of IndexOf of Array in Flex, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn