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

前端 未结 7 1529
甜味超标
甜味超标 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:44

    i++, ++i, and i+1 are all different expressions (or operations). You should consult your favourite Java reference to learn about the difference.

    (In short: i++ increments i but returns the original value, ++i increments i and returns the new value, i+1 does not increment i but returns the value of i+1.)

    As to why exactly your loop does not work the way you expect it to: this can not be answered without actually seeing your code—quite logical. My assumption would be that you either used the expressions wrong and/or that you incremented i twice per loop.

提交回复
热议问题