基础在for循环在ActionScript 3的弹性弹性、基础、for、ActionScript

2023-09-08 15:24:05 作者:心里除了你,还是你

早安计算器... 我有一个问题....这是我的样本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.