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

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

    array[i++] means "take the array element with index i, then increment i". array[i+1] means "take the array element with index i+1, do not modify i".

    0 讨论(0)
  • 2021-01-13 23:29
    • i++: increase the value stored in i by one, and return the old value.
    • ++i: increase the value stored in i by one, and return the new value.
    • i+1: return the sum of i and 1, without changing the value stored in i

    Now consider what happens if, as I suspect, you have code that looks like

    for ( i = 0; i < something; i++ )
    {
       dosomething(i++);
    }
    

    the values of i passed to dosomething() will be 0 2 4 8 . .

    since every iteration of the loop, i is incremented once in the for() line and once in the dosomething() line, and the dosomething() call is given the value i had before it was incremented. If the behaviour actually desired is for dosomething() to be called with the sequence 1 2 3 4 . . then you need to avoid updating i with the result of adding 1 to it in the loop body: for ( i = 0; i < something; i++ ) { dosomething(i+1); }

    or even

    for ( i = 1; i < (something+1); i++ )
    {
       dosomething(i);
    }
    
    0 讨论(0)
  • 2021-01-13 23:30

    array[i++] will give you the same as array[i] and then increment i. array[++i] will give you the same as array[i+1] and increment i. array[i+1] won't actually change the value of i.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-13 23:41
    • array[i++] will evaluate array[i] and increment i.
    • array[++i] will increment i and then evaluate array[i] (so it gives you the next element)
    • array[i+1] will give you the next element without incrementing i

    Personally I try to avoid using side-effects like this - it means when I read the code later, I always have to slow down to make sure I get everything in the right order.

    If you're already in a loop which is incrementing i, then incrementing i in the array access expression as well would mean that each loop iteration would increment i twice.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题