1

I have an array of doubles:

double[] arrayOfDoubles = new double[]{doubleVal1, doubleVal2, doubleVal3, doubleVal4, doubleVal5, doubleVal6};

and doubleValues initialization:

double doubleVal1 = 0;
double doubleVal2 = 0;
double doubleVal3 = 0;
double doubleVal4 = 0;
double doubleVal5 = 0;
double doubleVal6 = 0;

and now im using foreach loop with that array:

for (double val : arrayOfDoubles)
{
    val += 0.1;
}

i thinked it should add to every value(doubleVal1, doubleVal2..., doubleVal6) value 0.1.

What should i change to get that?

mmm
  • 260
  • 4
  • 21

5 Answers5

4

You can't do that with the advanced for loop, since val contains a copy of a value taken from the array, so modifying it doesn't modify the array.

Use a regular for loop instead:

for (int i = 0; i <  arrayOfDoubles.length; i++)
{
    arrayOfDoubles[i] += 0.1;
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • its just not possible using foreach loop? – mmm May 12 '17 at 09:53
  • Awesome, i didn't know val conatins a copy of a value. Thanks. – mmm May 12 '17 at 09:55
  • @MadMike6661 Not unless you create a counter variable, increment it within the loop, and use it to update arrayOfDoubles[i] instead of val. You might as well use a normal for loop. – Eran May 12 '17 at 09:55
  • It is true. You can literally see that there is a new 'double val' created for each loop-increment. – M. Haverbier May 12 '17 at 09:57
3

Use a regular for loop, not a foreach, so that you can access the indexes of the array .

for (int i = 0 ; i<arrayOfDoubles.length; i++)
{
    arrayOfDoubles[i] += 0.1;
}

Also have a look at : Is Java “pass-by-reference” or “pass-by-value”?

Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44
2

You can use foreach like this instead :

int i = 0;
for (double val : arrayOfDoubles) {
    arrayOfDoubles[i] += 0.1;
    i++;
}

Because for each in reality create a temp variable and not use the real values of array, it work like this

for (int i = 0; i < arrayOfDoubles.length; i++) {
    double val = arrayOfDoubles[i];
    //      ^^---------------------------------for each create a temp variable like this
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    Really nice answer using the equivalent code! Note that this would not be the problem with non-primitive object (unless create new instance or immutable instance) ;) – AxelH May 12 '17 at 10:00
1

You should change your loop from the enhanced for loop to regular for loop, because loop variable modifications do not reflect on the original item:

for (int i = 0 ; i != arrayOfDoubles.length ; i++) {
    arrayOfDoubles[i] += 0.1;
}

Note that this change would still have no effect on the fields doubleVal1..doubleVal6, which are copied into arrayOfDoubles on initialization. You cannot modify these without either referencing them directly, or using reflection (definitely not recommended for this task).

If your ultimate goal is to modify fields doubleVal1..doubleVal6, consider restructuring your class in a way that places these fields into an array permanently.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I guess the point is to change the variables values, not the array's values. But this is not possible with primitives.

You can create a simple class holding the value, but I'm sure there is a better design approach for your task:

class DoubleHolder {
    double val;
}

DoubleHolder doubleVal1 = new DoubleHolder();
DoubleHolder doubleVal2 = new DoubleHolder();
DoubleHolder doubleVal3 = new DoubleHolder();
DoubleHolder doubleVal4 = new DoubleHolder();
DoubleHolder doubleVal5 = new DoubleHolder();
DoubleHolder doubleVal6 = new DoubleHolder();

DoubleHolder[] arrayOfDoubles = {doubleVal1, doubleVal2, doubleVal3, doubleVal4, doubleVal5, doubleVal6};

for (DoubleHolder dh : arrayOfDoubles) {
    dh.val += 0.1;
}

System.out.print(doubleVal3.val);      // prints 0.1
ttulka
  • 10,309
  • 7
  • 41
  • 52