为什么 for-each 循环不允许递增 Integer?
我的意思是在这段代码中:
List<Integer> list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
for (Integer i : list)
i++;
System.out.println(list.get(0))
返回 1 而不是 2。在 for-each 循环中,Java 创建新对象 (i) 并从 List 中的对象复制字段值?
I mean in this code:
List<Integer> list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
for (Integer i : list)
i++;
System.out.println(list.get(0))
returns 1 not 2. In for-each loop Java creates new object (i) and copies fields value from object in List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
整数
是不可变的。您的代码将
i
变量更改为指向具有更大值的全新Integer
实例。列表中的原始 Integer 实例不会(也不能)更改。
Integer
s are immutable.Your code changes the
i
variable to point to a brand-newInteger
instance with a larger value.The original
Integer
instance in the list is not (and cannot be) changed.++ 运算符不是 Integer 对象上的有效运算符,因此 Java 使用其自动装箱功能将 Integer 对象转换为 int 原语。一旦转换,int 基元就会递增。您不保存原语,因此它会丢失。
为了实现这个目标,你需要做类似的事情
上面的代码不是最优的;但它不使用自动装箱。优化的解决方案将使用 ListIterator (根据大众需求添加):^)
请注意,此循环大量使用自动装箱,如果您希望了解自动装箱在幕后的作用,下面介绍的等效解决方案不依赖于任何自动装箱。
the ++ operator is not a valid operator on the Integer object, so Java uses it's auto-boxing features to convert the Integer object into an int primitive. Once it's converted, the int primitive is incremented. You don't save the primitive so it is lost.
To achieve the goal you need to do something like
The code above is not optimal; but it doesn't use autoboxing. An optimized solution would use a ListIterator (added by popular demand) :^)
Note that this loop uses autoboxing heavily, if you wish to get an idea of what autoboxing is doing under the covers, the equivalent solution presented below doesn't rely on any autoboxing.
它确实允许这样做,但它不会做你认为它会做的事情。
你所拥有的是简写。
编辑:更好的选择是使用 ListIterator,而不是使用 get(i) 和 set(i, value),这对于 LinkedList 来说可能非常昂贵。
It does allow it, it doesn't do what you think it does.
What you have is shorthand for.
EDIT: rather than using get(i) and set(i, value) which can be very expensive for LinkedList, a better choice would be to use ListIterator.
因为列表中的第 0 个元素是“1”,您在第 2 行中添加了它。
哦,我明白了:您想要增加整数。您必须这样做:
请注意,Integer 实例是不可变的。数值不能改变!
Because the 0th element in the list is the "1", which you added in line 2.
Oh, I understand: You want to increment the integers. You have to do:
Please note that Integer instances are immutable. The value cannot change!
因为
Integer
是不可变的,就像String
等。整数上的运算符
++
就像做:你需要 < code>list.set(index, i); 修改列表中的值。
Because
Integer
is immutable, just likeString
, etc.The operator
++
on an integer is just like doing :You'll need to
list.set(index, i);
to modify the value in your list.由于所有答案都与不变性有关,因此可变类型的外观如下:
这应该输出
2
。 (AtomicInteger 另外具有这些线程安全属性,但我们在这里只需要可变性。)基元类型
int
(与所有基元一样)也是不可变的,因此以下变体仍然会输出1:
Since all the answers are about immutability, here how it would look with a mutable type:
This should output
2
. (AtomicInteger additionally has these thread-safety properties, but we need here only the mutability.)The primitive type
int
(as all primitives) is immutable, too, so the following variant would still output1
: