开发者

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

开发者 https://www.devze.com 2023-04-01 06:31 出处:网络
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 my

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

精彩评论

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