java - Why for-each loop does not allow increment Integer? -
i mean in code:
list<integer> list = new linkedlist(); list.add(1); list.add(2); list.add(3); (integer : list) i++; system.out.println(list.get(0))
returns 1 not 2. in for-each loop java creates new object (i) , copies fields value object in list?
integer
s immutable.
your code changes i
variable point brand-new integer
instance larger value.
the original integer
instance in list not (and cannot be) changed.
Comments
Post a Comment