java Java在foreach中修改元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11032235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:31:30  来源:igfitidea点击:

Java Modifying Elements in a foreach

javaarraysforeachautoboxing

提问by Xenoprimate

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

我正在自己学习 Java;因此下面的代码除了学习/测试之外没有其他功能。

Essentially I'm trying to modify the elements of an Integer array (namely, halving them) whilst in a foreach loop.

本质上,我试图在 foreach 循环中修改 Integer 数组的元素(即,将它们减半)。

I should note that I'm not re-ordering, adding, or deleting elements; simply changing their values.

我应该注意,我没有重新排序、添加或删除元素;只是改变他们的价值观。

Here is my code:

这是我的代码:

Logger.describe("Now copying half of that array in to a new array, and halving each element");
Integer[] copyArray = new Integer[DEFAULT_SAMPLE_SIZE / 2];     
System.arraycopy(intArray, 0, copyArray, 0, DEFAULT_SAMPLE_SIZE / 2);
for (Integer x : copyArray) x /= 2;
Logger.output(Arrays.deepToString(copyArray));

However, the original array (intArray) is this:

但是,原始数组(intArray)是这样的:

[47, 31, 71, 76, 78, 94, 66, 47, 73, 21]

And the output of copyArray is:

copyArray 的输出是:

[47, 31, 71, 76, 78]

So although the array has been halved in size, the elements (Integers) haven't also been halved in value. So what am I doing wrong?

因此,尽管数组的大小已减半,但元素(整数)的值并未减半。那我做错了什么?

Thank you

谢谢

回答by Mattias Isegran Bergander

You can't do that in a foreach loop.

你不能在 foreach 循环中做到这一点。

for (int i=0; i<copyArray.length;i++)
    copyArray[i] /= 2;

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

否则你不会将它分配回数组。Integer顺便说一下,对象是不可变的,所以不能修改它们(尽管创建新的)。

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);

回答by Autar

for (int i = 0; i< copyArray.length; i++) {
    copyArray[i] = new Integer(x /2);
}

should work.

应该管用。

回答by Ravi Jain

int counter = 0;
for(int x : copyArray)
{
        x /= 2;
        copyArray[counter++] = x;
}

Your program just modified the value of variable x, not the values within the blocks of array copyArray

您的程序只是修改了变量x的值,而不是数组块中的值copyArray

回答by pvlissidis

I think that you can NOT use the foreach loop construct in order to modify the elements of the array you are iterating. Instead, you need to use a classic for loop like so:

我认为您不能使用 foreach 循环构造来修改您正在迭代的数组的元素。相反,您需要像这样使用经典的 for 循环:

Logger.describe("Now copying half of that array in to a new array, and halving each element");
Integer[] copyArray = new Integer[DEFAULT_SAMPLE_SIZE / 2];     
System.arraycopy(intArray, 0, copyArray, 0, DEFAULT_SAMPLE_SIZE / 2);
    for (int i = 0; i < copyArray.length; i++) {
        copyArray[i] /= 2;
    }
Logger.output(Arrays.deepToString(copyArray));