要找出指定值得数组元素
解决办法
用for 语句和break 语句就能找到匹配的元素。另外用ArrayUtilities.findMatchIndex( ),ArrayUtilities.findLastMatchIndex( ), 和ArrayUtilities.findMatchIndices( ) 方法
讨论
用for循环查找第一个匹配的元素后,用break立即返回,这样就实现功能了。
break应该在if语句里进行判断,是否找到匹配元素,找到则执行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 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 包中,首先导入它:
ArrayUtilities 类有三个方法来查找匹配的元素findMatchIndex( ), findLastMatchIndex( ), 和findMatchIndices( )。findMatchIndex( ) 方法至少需要两个参数:一个指向数组的引用和需要匹配的值,返回第一个匹配的元素下标,如果找不到返回-1:
trace(ArrayUtilities.findMatchIndex(letters, "b"));
// 显示: 1
trace(ArrayUtilities.findMatchIndex(letters, "r"));
// 显示: -1
也可以指定搜索的起始下标作为第三个参数:
trace(ArrayUtilities.findMatchIndex(letters, "a", 1));
// 显示: 4
如果第三个参数为true,则返回部分匹配的元素:
trace(ArrayUtilities.findMatchIndex(words, "s", true));
// 显示: 1
如果你想部分匹配又想指定起始搜索下标,可以把起始下标作为第四个参数。
findLastMatchIndex( ) 方法返回最后一个匹配的元素下标
findMatchIndices( ) 方法返回所有匹配的元素下标数组:
trace(ArrayUtilities.findMatchIndices(letters, "b"));
// 显示: 1,5
也可以设定为部分匹配,指定第三个参数为true:
trace(ArrayUtilities.findMatchIndices(words, "b", true));
// 显示: 0,1,3
ArrayUtilities 方法内部也是用for循环来实现的,现在我们看看代码,下面是findMatchIndex( )方法的代码:
// 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;
}