基础在for循环在ActionScript 3的弹性 [英] Basics in for loop in actionscript 3 in flex

查看:131
本文介绍了基础在for循环在ActionScript 3的弹性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早安计算器... 我有一个问题....这是我的样本code

Good Morning stackoverflow... I'm having a problem.... this is my sample code

var i:Number = new Number();

trace("showarray length" + showArray.length);

for(i=0;i<showArray.length;i++){

    trace("equal daw" + showArray.getItemAt(i).id + "==" + num);

    if(showArray.getItemAt(i).id == num){
        showArray.removeItemAt(i);

    }
}
trace('alerts');

myproblem这里是...... wherenever的,如果满足停止循环立即熄灭循环

myproblem here is...wherenever the if is satisfied it stops looping it immediately goes out of the loop

这是一个示例输出 鉴于showArray的长度为2和num = 0

this is a sample output given that the length of showArray is 2 and num = 0

showarray长度2

showarray length2

等于daw0 == 0

equal daw0==0

警告

请帮我

推荐答案

如果您要删除的项目在遍历数组,迭代顺序相反。这样元素的移除不影响周期条件:

If you want to remove items while iterating over array, iterate in reverse order. This way element removal does not affect cycle condition:

for (var i:int = showArray.length - 1; i >= 0; i--) {
    if (someCondition) {
        showArray.removeItemAt(i);
    }
}

另一个小奖金,这是稍快的,因为它不调用showArray.length每个步骤

Another small bonus that this is slightly faster, as it doesn't call showArray.length on each step.

这篇关于基础在for循环在ActionScript 3的弹性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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