Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the foreach statement not change the element value?

Tags:

java

foreach

How come the following prints boss and not bass?

String boss = "boss";
char[] array = boss.toCharArray();

for(char c : array)
{
 if (c== 'o')
     c = 'a'; 
}
System.out.println(new String(array)); //How come this does NOT print out bass?It prints boss.
like image 363
Kacy Raye Avatar asked Apr 05 '13 22:04

Kacy Raye


People also ask

How do I change the value of a forEach?

To change the value of all elements in an array: Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

Can the elements of an array be updated using a foreach loop?

forEach does not modify the array itself, the callback method can. The method returns undefined and cannot be chained like some other array methods. forEach only works on arrays, which means you need to be a little creative if you want to iterate over objects.

Can you change a foreach loop?

Why C#'s foreach loop cannot change what it loops over. With a foreach loop we easily iterate over all elements in a collection. During each pass through the loop, the loop variable is set to an element from a collection.

What does the forEach statement do?

The foreach statement: enumerates the elements of a collection and executes its body for each element of the collection. The do statement: conditionally executes its body one or more times.


6 Answers

You're changing the iteration variable c. That doesn't change the contents of the array. The iteration variable is just a copy of the array element. If you want to modify the array, you need to do so explicitly:

for (int i = 0; i < array.length; i++) {
    if (array[i] == 'o') {
        array[i] = 'a';
    }
}

Your original code is equivalent (as per section 14.14.2 of the JLS) to:

for (int i = 0; i < array.length; i++) {
    char c = array[i];
    if (c == 'o') {
        c = 'a'; 
    }
}

Changing the value of a local variable will never change anything else - it just changes the local variable. The assignment:

char c = array[i];

copies the value in the array into a local variable. It doesn't associate the local variable with the array element perpetually.

like image 149
Jon Skeet Avatar answered Oct 05 '22 06:10

Jon Skeet


This is because c = 'a' is assigning a to the local variable c which is not referencing the actual value present at that index of the array itself. It is just containing a copy of the value present at the specified index of array. So the change is actually made in the local variable not in the actual location where array[i] is referencing.. If you want to change value you should use the following indeed:

int i = 0;
for(char c : array)
{
 if (c== 'o')
     array[i] = 'a'; 
  i++;
}
like image 29
Vishal K Avatar answered Oct 05 '22 06:10

Vishal K


c's value is a copy of the value in array. Access array directly to change the value in question.

like image 25
dan Avatar answered Oct 05 '22 06:10

dan


You variable c gets changed, but not the array contents. To change the array, don't use c, manipulate the array directly.

for(int i = 0; i < array.length; i++)
{
 char c = array[i];
 if (c== 'o')
     array[i] = 'a';
}
like image 41
rgettman Avatar answered Oct 05 '22 05:10

rgettman


You're assigning 'a' to the local variable c, but not to the array element. To make it print bass, you'd need

for (int i = 0; i < array.length; i++) {
    if (array[i] == 'o') {
        array[i] = 'a';
    }
}
like image 27
JB Nizet Avatar answered Oct 05 '22 05:10

JB Nizet


Changes applied in 'for each' loop are made just inside her body (that's because values are copied, not referentions). To work on referentions use 'for' loop.

like image 31
Michał Tabor Avatar answered Oct 05 '22 06:10

Michał Tabor