Java Modifying Elements in a foreach

前端 未结 4 1176
迷失自我
迷失自我 2020-12-04 02:18

I\'m learning Java on my own; and therefore the code below has no function other than for learning/testing.

Essentially I\'m trying to modify the elements of an Inte

4条回答
  •  孤街浪徒
    2020-12-04 03:02

    You can't do that in a foreach loop.

    for (int i=0; i

    Else you are not assigning it back into the array. Integer objects are immutable by the way so can't modify them (creating new ones though).

    Updated from comment: Beware though that there are a few things going on, autoboxing/unboxing for example, roughly:

    copyArray[i] = Integer.valueOf(copyArray[i].intValue()/2);
    

提交回复
热议问题