Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Iterate over array, changing current object?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by slimchance; May 13th, 2012 at 07:54 PM.


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello... The reason that method A works is because it is modifying a variable that is in scope for that life of the method, and modifying the value in the reference of temp... 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.

    Understand?

    Let me know...

    Michael.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 .

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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:
    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.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 315 Times in 295 Posts

    Default Re: Iterate over array, changing current object?

    Quote Originally Posted by MES Enterprises LLC View Post
    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.
    Last edited by aussiemcgr; May 14th, 2012 at 03:07 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Iterate over array, changing current object?

    Quote Originally Posted by aussiemcgr View Post
    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.
    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.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 315 Times in 295 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Iterate over array, changing current object?

    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.

Similar Threads

  1. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 02:18 PM
  4. [SOLVED] How to Iterate through a string?
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 8
    Last Post: November 2nd, 2010, 05:45 PM
  5. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 05:39 PM