---
title: "搜索匹配的数组元素(转)"
author: "wdbk123"
date: "2010-04-15T16:58:06Z"
tags: ["C", "C#", "C++"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-04-15T16:58:06Z"
id: 644790
url: "https://wdbk123.iteye.com/blog/644790"
---

要找出指定值得数组元素  
**解决办法**  
用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 &lt; 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 &gt;= 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

也可以指定搜索的起始下标作为第三个参数：

var letters:Array = \["a", "b", "c", "d", "a", "b", "c", "d"];  
trace(ArrayUtilities.findMatchIndex(letters, "a", 1));  
// 显示: 4

如果第三个参数为true，则返回部分匹配的元素：

var words:Array = \["bicycle", "baseball", "mat", "board"];  
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

也可以设定为部分匹配，指定第三个参数为true：

var words:Array = \["bicycle", "baseball", "mat", "board"];  
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 &lt; 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;  
}