Can anyone explain me why such call doesn't increment my i
value ?
int i = 0;
list.get(7 + (i = i++));
list.get(7 + (i = i++));
it leaves i=0
instead of increment by one at least such that in the second call it is 1.
i = i++
就像在做:
int old_i = i;
i = i + 1;
i = old_i;
实际发生的是i++
的值是在增量发生之前的值,然后i
将i
获得...的值i
。
在一行i++
中将使用旧值,i
然后将其递增。
i = i++
分配第一个并增加第二个
继承人的执行本质上是这样的:
list.get(7 + (i = i)); //list.get(7);
i = i + 1; //i = 1
list.get(7 + (i = i); //list.get(8);
i = i + 1; //i = 2
++i
将首先增加变量并分配第二个
i = i++
意味着i
将分配旧值i
,然后将增加 1。如果你想真正增加你的值,你应该写:
int i = 0;
list.get(7 + (i++));
list.get(7 + (i++));
或者:
int i = 0;
list.get(7 + (i+1));
list.get(7 + (i+2));