- 浏览: 128207 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
lliiqiang:
NetConnection.Connect.Rejected拒 ...
第一个FMS程序,连接到FMS服务器(Flash/Flex两种实现) 转
要找出指定值得数组元素
解决办法
用for 语句和break 语句就能找到匹配的元素。另外用ArrayUtilities.findMatchIndex( ),ArrayUtilities.findLastMatchIndex( ), 和ArrayUtilities.findMatchIndices( ) 方法
讨论
用for循环查找第一个匹配的元素后,用break立即返回,这样就实现功能了。
break应该在if语句里进行判断,是否找到匹配元素,找到则执行break推出循环,否则继续查找。
var letters:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];
// 指定要搜索的内容
var match:String = "b";
for (var i:int = 0; i < letters.length; i++) {
// 检测当前元素是否匹配
if (letters[i] == match) {
trace("Element with index " + i +
" found to match " + match);
break;
}
}
// 指定要搜索的内容
var match:String = "b";
for (var i:int = 0; i < letters.length; i++) {
// 检测当前元素是否匹配
if (letters[i] == match) {
trace("Element with index " + i +
" found to match " + match);
break;
}
}
也可以找到匹配的最后一个元素,这就需要倒序遍历数组:
var letters:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];
var match:String = "b";
for (var i:int = letters.length - 1; i >= 0; i--) {
if (letters[i] == match) {
trace("Element with index " + i +
" found to match " + match);
break;
}
}
var match:String = "b";
for (var i:int = letters.length - 1; i >= 0; i--) {
if (letters[i] == match) {
trace("Element with index " + i +
" found to match " + match);
break;
}
}
使用自定义类ArrayUtilities 类更简单,它在ascb.util 包中,首先导入它:
import ascb.util.ArrayUtilities;
ArrayUtilities 类有三个方法来查找匹配的元素findMatchIndex( ), findLastMatchIndex( ), 和findMatchIndices( )。findMatchIndex( ) 方法至少需要两个参数:一个指向数组的引用和需要匹配的值,返回第一个匹配的元素下标,如果找不到返回-1:
var letters:Array = ["a", "b", "c", "d"];
trace(ArrayUtilities.findMatchIndex(letters, "b"));
// 显示: 1
trace(ArrayUtilities.findMatchIndex(letters, "r"));
// 显示: -1
trace(ArrayUtilities.findMatchIndex(letters, "b"));
// 显示: 1
trace(ArrayUtilities.findMatchIndex(letters, "r"));
// 显示: -1
也可以指定搜索的起始下标作为第三个参数:
var letters:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];
trace(ArrayUtilities.findMatchIndex(letters, "a", 1));
// 显示: 4
trace(ArrayUtilities.findMatchIndex(letters, "a", 1));
// 显示: 4
如果第三个参数为true,则返回部分匹配的元素:
var words:Array = ["bicycle", "baseball", "mat", "board"];
trace(ArrayUtilities.findMatchIndex(words, "s", true));
// 显示: 1
trace(ArrayUtilities.findMatchIndex(words, "s", true));
// 显示: 1
如果你想部分匹配又想指定起始搜索下标,可以把起始下标作为第四个参数。
findLastMatchIndex( ) 方法返回最后一个匹配的元素下标
findMatchIndices( ) 方法返回所有匹配的元素下标数组:
var letters:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];
trace(ArrayUtilities.findMatchIndices(letters, "b"));
// 显示: 1,5
trace(ArrayUtilities.findMatchIndices(letters, "b"));
// 显示: 1,5
也可以设定为部分匹配,指定第三个参数为true:
var words:Array = ["bicycle", "baseball", "mat", "board"];
trace(ArrayUtilities.findMatchIndices(words, "b", true));
// 显示: 0,1,3
trace(ArrayUtilities.findMatchIndices(words, "b", true));
// 显示: 0,1,3
ArrayUtilities 方法内部也是用for循环来实现的,现在我们看看代码,下面是findMatchIndex( )方法的代码:
public static function findMatchIndex(array:Array, element:Object):int {
// Use a variable to determine the index
// from which to start. Use a default value of 0.
var startingIndex:int = 0;
// By default don't allow a partial match.
var partialMatch:Boolean = false;
// If the third parameter is a number,
// assign it to nStartingIndex.
// Otherwise, if the fourth parameter is a number,
// assign it to nStartingIndex instead.
if(typeof arguments[2] == "number") {
startingIndex = arguments[2];
}
else if(typeof arguments[3] == "number") {
startingIndex = arguments[3];
}
// If the third parameter is a Boolean value,
// assign it to partialMatch.
if(typeof arguments[2] == "boolean") {
partialMatch = arguments[2];
}
// Assume no match is found.
var match:Boolean = false;
// Loop through each of the elements of the array
// starting at the specified starting index.
for(var i:int = startingIndex;
i < array.length; i++) {
// Check to see if the element either matches
// or partially matches.
if(partialMatch) {
match = (array[i].indexOf(element) != -1);
}
else {
match = (array[i] == element);
}
// If the element matches, return the index.
if(match) {
return i;
}
}
// The following return statement is only reached
// if no match was found. In that case, return -1.
return -1;
}
public static function findMatchIndices(array:Array,
element:Object, partialMatch:Boolean = false):Array {
var indices:Array = new Array( );
var index:int = findMatchIndex(array,
element,
partialMatch);
while(index != -1) {
indices.push(index);
index = findMatchIndex(array,
element,
partialMatch,
index + 1);
}
return indices;
}
// Use a variable to determine the index
// from which to start. Use a default value of 0.
var startingIndex:int = 0;
// By default don't allow a partial match.
var partialMatch:Boolean = false;
// If the third parameter is a number,
// assign it to nStartingIndex.
// Otherwise, if the fourth parameter is a number,
// assign it to nStartingIndex instead.
if(typeof arguments[2] == "number") {
startingIndex = arguments[2];
}
else if(typeof arguments[3] == "number") {
startingIndex = arguments[3];
}
// If the third parameter is a Boolean value,
// assign it to partialMatch.
if(typeof arguments[2] == "boolean") {
partialMatch = arguments[2];
}
// Assume no match is found.
var match:Boolean = false;
// Loop through each of the elements of the array
// starting at the specified starting index.
for(var i:int = startingIndex;
i < array.length; i++) {
// Check to see if the element either matches
// or partially matches.
if(partialMatch) {
match = (array[i].indexOf(element) != -1);
}
else {
match = (array[i] == element);
}
// If the element matches, return the index.
if(match) {
return i;
}
}
// The following return statement is only reached
// if no match was found. In that case, return -1.
return -1;
}
public static function findMatchIndices(array:Array,
element:Object, partialMatch:Boolean = false):Array {
var indices:Array = new Array( );
var index:int = findMatchIndex(array,
element,
partialMatch);
while(index != -1) {
indices.push(index);
index = findMatchIndex(array,
element,
partialMatch,
index + 1);
}
return indices;
}
发表评论
-
Flex 对Java端返回Collection的处理方法
2010-07-20 22:21 1044将Flex与Spring集成后(BlazeDS 与Spring ... -
在文本框里添加超链接
2010-05-31 10:16 2162问题我想在文本框里加入超链接解决办法利用HTML <a ... -
BulkLoader类和PV3D
2010-05-31 10:06 883AS3取消了movieclip的loader类,所以加载图 ... -
Flex LinkButton仿IE超链接
2010-05-31 10:02 2967重写的LinkButton类Kelvin_LinkButt ... -
flex RSL做资源共享管理
2010-05-18 10:35 1045Flex SDK 里有一个运行时组件共享库——RSL ( Ru ... -
很不错的flex果冻特效 转
2010-05-13 10:36 1795index.mxml <?xml version ... -
Flex 特效一部分 转
2010-05-12 14:24 14571:glow(发光) 代码: <mx:Glow id ... -
Flex3特效的基本用法 原理 转
2010-05-12 11:40 1259Flex3为开发人员提供了多种基本的特效控件,每种特效控件对应 ... -
Flex3特效的基本用法 特效组件 转
2010-05-12 11:34 1304Flex3中预定义了多种特效组件,每种组件对应于一种或多种特效 ... -
FLEX3中应用CSS完全详解手册! 转
2010-05-11 16:44 847在FLEX中使用样式方法一、使用本地样式定义使用<mx: ... -
Flex加载图片的常用的几种方式 转
2010-05-11 15:17 761<?xml version="1.0&qu ... -
flex特效 触发器
2010-04-30 16:00 1153触发器在Flex3的特效实现中起着重要作用,对于Flex3中的 ... -
数据筛选(转)
2010-04-15 17:16 691<?xml version="1.0" ... -
在数组首部和尾部添加元素(转)
2010-04-15 17:01 3185要再添加新元素到数组解决办法push( ) 方法在数组尾部添加 ... -
遍历数组成员(转)
2010-04-15 17:00 846要访问数组的每个元素 ... -
.删除数组元素(转)
2010-04-15 16:56 1408要删除一个或多个数组元素,或移动数组元素解决办法splice( ... -
在数组中间插入元素(转)
2010-04-15 16:53 2130要在数组中间插入元素解决办法使用splice( ) 方法讨论s ... -
读取关联数组(转)
2010-04-15 16:50 818怎样遍历关联数组解决办法使用for . . . in 语句讨论 ... -
创建关联数组(转)
2010-04-15 16:48 920要创建用名称元素作为索引的数组解决办法创建关联数组讨论用关联数 ... -
比较数组(转)
2010-04-15 16:45 903怎么判断两个数组是否相等呢解决办法循环数组,一一比较对应位置的 ...
相关推荐
byte数组中匹配特定byte数组,速度比Skip(k).Take(find.Length).SequenceEqual(find)快很多,小于3ms
实际中,经常需要用到对数组的操作,特别是一维数组、二维数组操作,labview中提供了搜索一维数组函数,但是没有提供搜索二维数组。本文章主要实现对字符串二维数组的搜索并返回指定内容。
这篇文章主要给大家介绍的是利用js查找数组中指定元素并返回该元素的所有索引的相关资料,文中给出了详细的示例代码,下面话不多说,来看看详细的代码示例吧。 示例代码 //在数组中查找所有出现的x,并返回一个包含...
5.3.搜索匹配的数组元素 66 5.4.删除数组元素 69 5.5.在数组中间插入元素 71 5.6.转换字符串为数组 71 5.7.转换数组为字符串 72 5.8.创建数组的拷贝 73 5.9.存储多维数据 75 5.10.数组排序 77 5.11.实现自定义排序 ...
如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。 在 PHP 4.2.0 之前,函数在失败时返回 null 而不是 false。 如果第三个参数 strict 被指定为 true,则只有在数据类型和值都一致时才返回相应...
反转数组元素的顺序。 从数组中删除第一个元素。 对数组的元素进行排序。 从数组中添加或删除元素。 在数组的前面添加一个或多个元素。 还有一些访问方法可以返回数组的某些表示形式: 将数组与其他数组或值连接在...
将数组元素连接为字符串.asp 构造制定字符串.asp 正向截取字符串.asp 正向查找字符串.asp 获取字符串长度.asp 第13章(/14/) Matches集合与Macth对象.asp ...
如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。 但是array_search一般用到搜索一个数组中符合要求的第一个字符串。如果搜索的字符串在数组中含有多个,使用array_search的话,是 不行的。这时...
• sample11.htm 使用delete运算符删除数组元素 • sample12.htm in运算符在对象中的使用 • sample13.htm in运算符在数组中的使用 • sample14.htm instanceof运算符 • sample15....
搜索算法:搜索算法用于在数据集中查找特定元素的算法。常见的搜索算法包括线性搜索、二分搜索等。 图算法:图算法用于处理图结构的数据,如最短路径算法(如Dijkstra算法、Floyd-Warshall算法)、最小生成树算法...
搜索算法:搜索算法用于在数据集中查找特定元素的算法。常见的搜索算法包括线性搜索、二分搜索等。 图算法:图算法用于处理图结构的数据,如最短路径算法(如Dijkstra算法、Floyd-Warshall算法)、最小生成树算法...
$("div#intro .head") id="intro" 的 <div> 元素中的所有 class="head" 的元素 三. jQuery 事件函数 jQuery 事件处理方法是 jQuery 中的核心函数。 事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。术语...
线性时间模式搜索算法的Z算法,在线性时间内查找...Z 数组的元素 Z[i]存储从字符串[i]开始的最长子串的长度,字符串[i]也是字符串[0]的前缀..n-1]。Z 数组的第一个条目意义不大,因为完整的字符串总是它自己的前缀。
实例42 数组元素算术运算 第5章 簇数据 实例基础 簇数据概述 实例43 捆绑 实例44 解除捆绑 实例45 按名称捆绑 实例46 按名称解除捆绑 实例47 创建簇数组 实例48 索引与捆绑簇数组 第6章 数据类型转换 实例基础 ...
5.10搜索和删除或替换数组元素 5.11对每个数组元素应用一个函数 5.12对数组中的每个元素执行一个函数并返回一个新数组 5.13创建一个过滤后的数组 5.14验证数组内容 5.15使用一个关联数组来存储表单元素...
1.1 从有序数组中删除重复元素 1.2 在排序数组被旋转后进行查找 1.3 寻找两个排序数组的中位数 1.4 最长连续序列 1.5 累加和 1.6 移除数组中指定值 1.7 下一个排列 1.8 第n个全排列 1.9 验证数独的正确性 1.10 容纳...
2. 如何重用元素搜索 3 3. 任何使用has()来检查某个元素是否包含某个类或是元素: 3 4. 如何使用jQuery来切换样式表 3 5. 如何限制选择范围(基于优化目的): 3 6. 如何正确地使用ToggleClass: 4 7. 如何设置IE...
21.8 元素不匹配查找mismatch 293 21.9 元素相等判断equal 295 21.10 子序列搜索search 296 21.11 重复元素子序列搜索search_n 299 21.12 最后一个子序列搜索find_end 301 21.13 本章小结 303 第22章 ...
' 并仅显示匹配元胞数组的 % 元素。 % 用户选择他/她想要的条目及其索引返回原始数组中的 % 元素。 % '*' 通配符代表任意数量(包括零)的字符。 % 这 '?' 通配符代表单个字符。 % % 用法: % IDX = WILDSEL...
在演示算法之前,需先在弹出的小窗口中输入线性表的数据元素及算法参数 i(插入或删除的位置)和 b(被插的数据元素)的值。顺序表的图示窗口在演示屏的上方,链表的图示窗口在左侧。 2. 有序表的操作 算法演示屏的...