Array collapse phenomenon

Array collapse phenomenon:

 

    When the operation of the array, the array length will make a difference, an array of simultaneous operation of next indexes that item will be skipped, resulting in an array is skipped, this phenomenon is called an array of collapse.

    E.g:

   Loop determination value of each item in the array, if it is equal to delete 4

   A first traverse to 4, the index value i is 3, a delete operation, at this time becomes a length of the array 14 from the original 15, the index value is 3.

   Performing cycle i ++, i is 4. In this case, it had not deleted the second array index value becomes 4 3, and again performs operation i = 4, thus skipping the original array without delete the second 4, to delete the original did not delete the array third 4, resulting in the output results have several 4 is not deleted, on the formation of an array of collapse phenomenon.

 Solution:

     1 is, after an operation of the array, the index value manually i -, each holding an array are traversed.

2. The loop through the array backwards

 

to sum up

Line has been nearly three months of work in the fight against the class, learned JS charm, but also a lot of things to learn, JS too difficult for a white terms, accept them is not very satisfactory. So sad. Hope can advise chiefs.

Guess you like

Origin www.cnblogs.com/zhlang/p/12643139.html