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?

integers 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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -