ArrayUtil class contains static utility methods for manipulating and working with Arrays. Note that all APIs assume that they are working with well formed arrays. I.e. they will only manipulate indexed values.

Methods:

1. arrayContainsValue():

public static arrayContainsValue(arr:Array,value:Object):Boolean

Parameters:

arr:Array The array that will be checked for specified value.

value:Object The object which will be searched for within the array.

Returns:

Boolean True if the array contains the value, False if it does not.

2. arraysAreEqual()

public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean

Compares two arrays and returns a boolean indicating whether the arrays contain the same values at the same indexes.

Parameters:

arr1:Array The first array that will be compared to the second.

Arr2:Array The second array that will be compared to the first.

Returns:

Boolean True if the arrays contains the same values at the same indexes.

False if they do not.

3. copyArray():

public static function copyArray(arr:Array):Array

Creates a copy of the specified array. Note that the array returned is a new array but the items within the array are not copies of the items in the original array (but rather references to the same items).

Parameters:

arr:Array The array that will be copies

Returns:

Array A new array which contains the same items as the array passed in.

4. createUniqueCopy()

public static function createUniqueCopy(a:Array):Array

Create a new array that only contains unique instances of objects in the specified array. Basically, this can be used to remove duplication object instances from an array

Parameters:

a:Array The array which contains the values that will be used to create the new array that contains no duplicate values.

Returns:

Array A new array which only contains unique items from the specified array.

5. removeValueFromArray()

public static function removeValueFromArray(arr:Array, value:Object):void

Remove all instances of the specified value from the array.

Parameters:

arr:Array — The array from which the value will be removed.

value:Object — The object that will be removed from the array.

Screenshot:

In this demo, “array1” is the same as “array2” and a little different with “array3”, the order of their elements is different.

Enter any value of the elements of array1 into the input box contained in the row which owns button “arrayContainsValue”. Then, click the button, a dialog pop up to show the result whether the element with the input value is contained in array1.

“arraysAreEqual” compares “array1” with “array2” and “array1” with “array3”.

“copyArray” copy “array1”and add a new TextInput to show the copied array.

“createUniqueArray” is similar as “copyArray”. But its elements are unique.

Enter the value of elements in Array1 into the input box beside “removeValueFromArray”, then, click the button to implement the method.

The element with value “5” is remove from array1.

The following is full source code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import com.adobe.utils.ArrayUtil;
import mx.controls.Alert;
[Bindable]
private var array1:Array = [1,2,3,4,5,6,7,8,9,10,11,22,22,33,34,34];
[Bindable]
private var array2:Array = [1,2,3,4,5,6,7,8,9,10,11,22,22,33,34,34];
[Bindable]
private var array3:Array = [1,2,3,4,5,6,7,8,9,10,22,11,22,34,33,34]; private var array:Array = [array1,array2,array3]; private function doArrayContainsValue():void
{
var msg:String = "does array1 contains value "+containedValue.text+"?";
msg += "\n"; msg += ArrayUtil.arrayContainsValue(array1,int(containedValue.text))?"yes":"no"; Alert.show(msg);
} private function doArraysAreEqual():void
{ var msg:String = "does array1 equals array2 ?";
msg += "\n";
msg += ArrayUtil.arraysAreEqual(array1,array2)?"yes":"no"; msg += "\n"; msg += "does array1 equals array3 ?";
msg += "\n";
msg += ArrayUtil.arraysAreEqual(array1,array3)?"yes":"no"; Alert.show(msg);
} private function doCopyArray():void
{
var newArray:TextInput = new TextInput();
newArray.editable = false;
newArray.percentWidth = 100; var copiedArray:Array = ArrayUtil.copyArray(array1);
newArray.text = "array"+ String((array.length+1))+ ":" + copiedArray.toString() + " copied from array1";
array.push(copiedArray); container.addChild(newArray); container.validateNow();
} private function doUniqueCopy():void
{
var newArray:TextInput = new TextInput();
newArray.editable = false;
newArray.percentWidth = 100; var copiedArray:Array = ArrayUtil.createUniqueCopy(array1);
newArray.text = "array"+ String((array.length+1))+ ":" + copiedArray.toString() + " copied from array1";
array.push(copiedArray); container.addChild(newArray); container.validateNow();
} private function doRemoveValueFromArray():void
{
if(ArrayUtil.arrayContainsValue(array1,int(removedValue.text)))
{
ArrayUtil.removeValueFromArray(array1,int(removedValue.text));
array1Text.text = "array1:"+ array1.toString();
}
else
{
var msg:String = "array1 does not contain value "+removedValue.text;
}
}
]]>
</mx:Script>
<mx:VBox id="container" width="100%" height="100%">
<mx:TextInput id="array1Text" editable="false" text="array1:{ array1.toString()}" width="100%"/>
<mx:TextInput editable="false" text="array2:{ array2.toString()}" width="100%"/>
<mx:TextInput editable="false" text="array3:{ array3.toString()}" width="100%"/>
<mx:HBox>
<mx:Label text="does array1 contains value:"/>
<mx:TextInput id="containedValue" text="5"/>
<mx:Button label="arrayContainsValue" click="doArrayContainsValue();"/>
</mx:HBox>
<mx:HBox>
<mx:Button label="arraysAreEqual" click="doArraysAreEqual();"/>
</mx:HBox>
<mx:HBox>
<mx:Button label="copyArray" click="doCopyArray();"/>
</mx:HBox>
<mx:HBox>
<mx:Button label="createUniqueCopy" click="doUniqueCopy();"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="remove value"/>
<mx:TextInput id="removedValue" text="5"/>
<mx:Button label="removeValueFromArray" click="doRemoveValueFromArray();"/>
</mx:HBox>
</mx:VBox>
</mx:Application>

最新项目地址: https://github.com/mikechambers/as3corelib

转自: http://ntt.cc/2008/09/01/as3corelib-tutorial-how-to-use-arrayutil-class-in-flex.html

最新文章

  1. Kubernets搭建Kubernetes-dashboard
  2. [WCF编程]12.事务:事务传播
  3. salesforce 零基础学习(十九)Permission sets 讲解及设置
  4. 我的fckeditor实践
  5. docker operation method note
  6. js的选择星级评分插件
  7. 如何做Gibbs采样(how to do gibbs-sampling)
  8. HDU-简单计算器-1237
  9. Socket 服务器和客户端通信
  10. stormzhang的推荐!
  11. oracle_五千万数据插入测试
  12. linux下的数据库管理工具phpmyadmin安装以及文件大小限制的配置修改
  13. 一张有料的图片!!!附文件-图片合成器C语言实现算法
  14. go mod代理和小技巧
  15. Mysql 数据类型、约束类型
  16. Excel分组快速自动填充编号
  17. Cracking The Coding Interview 2.5
  18. JavaScript之prototype对象
  19. Spring AsyncRestTemplate
  20. linux内核分析第三周-跟踪分析Linux内核的启动过程

热门文章

  1. 开发原生安卓cordova插件(有原生界面)
  2. Bloom Filter概念和原理【转】
  3. 【Win32汇编】编译环境配置
  4. 洛谷 P1413 坚果保龄球
  5. 语音行业技术领先者Nuance诚招ASR/NLP研发工程师和软件工程师
  6. 制作JPEGImages出现的bug
  7. ZOJ3228 Searching the String (AC自动机)
  8. Node.js实现简单的爬取
  9. jQuery的on绑定click和直接绑定click区别
  10. Python 函数的初识