Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Is there any way to modify object using enhanced for loop ?

    Assume I have an array holding something. ints, Integers, String or boolean values..

    Can I use the

    Java Code:
    for(Integer myInt: myIntArray)
    {
        // check myInt if it is less then 5
        // change the value of myInt
        // make the value that I had in myIntArray equal to the value that new myInt has
    }
    I hope my question is clear enough.
    Thank you.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Is there any way to modify object using enhanced for loop ?

    Instances of Integer are "immutable" in the sense that there is no way to modify them.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Is there any way to modify object using enhanced for loop ?

    But if it is some other mutuable object, then I am allowed to do it ?

    The reason is then because the "immutable" concept ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Is there any way to modify object using enhanced for loop ?

    if it is some other mutuable object, then I am allowed to do it
    If you can get a reference to the object, you can call its methods.
    There could be methods that change the object's contents.

Similar Threads

  1. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 02:07 PM
  2. Enhanced For Loop
    By terahawks in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 08:46 AM
  3. Help for Enhanced for loop
    By jboy in forum New To Java
    Replies: 6
    Last Post: 09-13-2009, 06:45 AM
  4. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •