发现在AS3数组序列 [英] finding sequences in AS3 array

查看:136
本文介绍了发现在AS3数组序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有任何人有任何想法,该怎么做才能找到序列中的数组数?

例如,我的数组是:

  VAR数字:阵列=新阵列(香蕉,香蕉,苹果,香蕉,香蕉);
 

和我需要找到的是: *多少次有香蕉的序列 *和每个序列的长度。

我做的,为了得到以下结果什么shell: 2,1,2(2香蕉,苹果1个,2个香蕉)

我试着用做while循环,但二想我错过了什么。

一个简单的例子将是非常美联社preciated!

感谢名单

解决方案

  VAR preV:字符串= NULL;
VAR问:INT = 0;
VAR结果:阵列=新的Array();
对于(VAR我:= 0; I< numbers.length ++我){
  如果(preV!=号[I]){
    如果(Q&0)result.push(q)的;
    Q = 1;
    preV =号[I]
  }
  其他++ q;
}
如果(Q&0)result.push(q)的;
 

这就是假设香蕉等都是字符串(上面可能是笔误?)。这将是简单修改到其它类型的对象

does anyone have any idea what to do in order to find the number of sequences in an array?

for example, my array is:

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.

what shell i do in order to get the following result: 2,1,2 (2 bananas, 1 apple, 2 bananas)

i tried with do while loop, but i i guess i miss something.

a short example will be very appreciated!

thanx

解决方案

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数组序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆