本文介绍了在 AS3 数组中查找序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人知道怎么做才能找到数组中的序列数?
does anyone have any idea what to do in order to find the number of sequences in an array?
例如,我的数组是:
var numbers:Array = new Array(banana, banana, apple, banana, banana);
我需要找到的是:* 有多少次香蕉"的序列* 以及每个序列的长度.
and i need to find is:* how many times there is a sequence of "banana"* and the length of each sequence.
为了得到以下结果我做了什么shell:2,1,2(2个香蕉,1个苹果,2个香蕉)
what shell i do in order to get the following result:2,1,2 (2 bananas, 1 apple, 2 bananas)
我尝试使用 do while 循环,但我想我错过了一些东西.
i tried with do while loop, but i i guess i miss something.
一个简短的例子将不胜感激!
a short example will be very appreciated!
谢谢
推荐答案
var prev:String = null;
var q:int = 0;
var result:Array = new Array();
for(var i:int=0; i<numbers.length; ++i){
if(prev!=numbers[i]){
if(q>0) result.push(q);
q=1;
prev=numbers[i];
}
else ++q;
}
if(q>0) result.push(q);
这是,假设香蕉等是字符串(可能是上面的错字?).修改为其他类型的对象会很简单
This is, assuming banana, etc. are strings (probably a typo above?). It would be simple to modify to other types of objects
这篇关于在 AS3 数组中查找序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!