Iterate over array, changing current object?
Hi!
I have a question regarding iterating over an array:
Why cant i use for each (Method B)?
String s="aaa222bbb";
char[] temp=s.toCharArray();
A
for(int i=0;i<temp.length;i++){
if (Character.isDigit(temp[i])) temp[i]='x' ;
else temp[i]='y';
}
B
for(char c:temp){
if (Character.isDigit(c)) c='x' ;
else c='y';
}
Thank you very much for answers
Re: Iterate over array, changing current object?
Hi
Thanks for the fast reply.
Yes i understand, and i thought this might be the case. Then, if i need to change the elements in an array, i should stick with plan A, create an iterator, or build a new list as i go through the for each?)
Thanks again :) .
Re: Iterate over array, changing current object?
I quite often use a pattern like this, though I suspect that it might not be to everyone's taste. I like it:
Code java:
int i = 0;
char[] theChars = theString.toCharArray()
for (char c : theChars)
theChars[i++] = (Character.isDigit(c) ? 'x' : 'y');
You get to use a foreach, but still have an index.
Re: Iterate over array, changing current object?
Quote:
Originally Posted by
MES Enterprises LLC
In method B, the variable 'c' only survives within the for-loop, and only holds a copy of the value in temp, not a reference to temp, therefore changing 'c' doesn't change temp.
Now, that is only true because char is a primitive, right? For Objects, it does refer to the Object, since you can envoke methods on it while inside the while loop that may make changes to the Object (thus it wouldn't be a copy, but the actual Object). Although, overwriting the Object with a new one probably wouldn't overwrite the Object in the array's index...or would it? lol.
Re: Iterate over array, changing current object?
Quote:
Originally Posted by
aussiemcgr
Now, that is only true because char is a primitive, right? For Objects, it does refer to the Object, since you can envoke methods on it while inside the while loop that may make changes to the Object (thus it wouldn't be a copy, but the actual Object). Although, overwriting the Object with a new one probably wouldn't overwrite the Object in the array's index...or would it? lol.
Code :
Object[] temp = {"foo", null, "bar"};
for(Object ob :temp){
if(ob == null) {
ob = "???";
} else {
ob = null;
}
The situation here is exactly the same as in (B). ob is a local variable whose value cannot be accessed outside the loop (just like char c). Assigning values to the variable have no effect on anything once the loop is finished, again just like c and, again because ob is a local variable.
"Overwriting the object with a new one" is a slightly slippery way of talking as objects don't get overwritten. I guess you mean "assign a new value to the variable" and this has no permanent effect as noted above.
-----
The value of ob is a reference to an object. And, yes, methods can be called which might alter the state of that object. But as far as variables and their values are concerned (the only things that might sensibly be compared in the two cases) the semantics is the same.
Re: Iterate over array, changing current object?
Well by "overwriting" I was referring to rewriting the Object's memory location. But to have my question answered: reassigning the FOR variable does nothing to the array (although envoking methods on the Object does effect the variable). Interesting. I understand why.
Re: Iterate over array, changing current object?
Quote:
although envoking methods on the Object does effect the variable
It's perhaps a nitpick, but it would be more accurate to say that invoking methods on a variable affects the state of the object referenced by that variable's value. Invoking methods never changes the variable itself: that is to say its value remains the same.
A commonly held wrong end of the stick is to think of objects as the values of variables. But they're not: the values of variables are references (or pointers) to objects. An object might change its state while something pointing (or referring) to it does not change.