flex 中 actionscript 3 for 循环的基础知识 [英] Basics in for loop in actionscript 3 in flex

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

问题描述

早上好 stackoverflow...我遇到了问题......这是我的示例代码

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');

我的问题是……只要满足 if 就停止循环,它会立即退出循环

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 length2

showarray length2

等于 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.

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

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