loop's counter i as I++ vs. i+1 as position in an array

前端 未结 7 1531
甜味超标
甜味超标 2021-01-13 23:25

I\'ve made a loop with i as it\'s counter variable.
Inside that loop I\'m comparing cells of an array.
I\'m wondering what\'s the difference between array[i++] (or a

7条回答
  •  深忆病人
    2021-01-13 23:37

    Not working properly because you are incrementing i twice in each loop.

    So if your array is {1,2,3,4,5,6,7} and you printed array[++i] starting at i=0, it will print "2,4,6," then throw an ArrayOutOfBoundsExcpetion, if the array is {1,2,3,4,5,6,7,8} it will print "2,4,6,8"

    Better be away from ++ on the loop counter, except if you really mean it.

提交回复
热议问题