开发者

How to delete an element from an array, which is in looping ?

Following is a psuedo-code example of what I am trying to do. I loop over an array and delete elements bas开发者_运维技巧ed on a condition. Now the problem is when I remove the elements the size of myArray is changing, and I get an out of index exception. Any ideas on how to get around this?

for i = 1:size(myArray)
    if myArray(i) == someValue
        remove_from(myArray, i)


a simple solution for this problem is loop from the last to the first element.

for i = size(myArray):1
    if myArray(i) == someValue
        remove_from(myArray, i)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜