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 5 of 5

Thread: Postfix in array[i++] seems effect my expecting result

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Postfix in array[i++] seems effect my expecting result

    Hi All,
    I have a newbie question which makes me struggle couple hours and search online for nothing. will you please kindly help me on this? i have no one to ask since i am study by my own. thank you for your time and support.

    		char array[] = new char[10];
     
    		for (int i = 0; i < 10;){
    			array[i++] = (char)('A' + i); 
    			System.out.println("char for array is: " + array[0]);
    		}
    I don't understand that in the beginning of the for loop, "i" was init to 0 and (char) ('A' + i) remain 'A' which was assigned to array[0].
    However, the result for array[0] is B. Not A.

    can anyone welling to share the opinion? appreciate for your help.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Postfix in array[i++] seems effect my expecting result

    You are incrementing i inside the loop. You should try to avoid changing the value of your loop control variable inside the loop as it can have unexpected side effects as you have found out. Increment in the loop header like it should be and it will work.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    sheng8 (August 16th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Postfix in array[i++] seems effect my expecting result

    Thank you for the suggestion and appreciate for the help. i did try to avoid the increment i in the loop and it works fine.
    if something like non-define element and try to print it out, it may shows unexpected value which i understand. but in my case, i really want to know the reason of storing B in array[0] instead of A.

    and i also remove the loop as follow, same thing happen:
    		char array[] = new char[10];
    	        int i = 0;
    		array[i++] = (char)('A' + i); 
    		System.out.println("char for array is: " + array[0]);
    It will be nice to have the opinion, thanks again and wish the best
    Last edited by sheng8; August 16th, 2014 at 01:02 AM.

  5. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    623
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: Postfix in array[i++] seems effect my expecting result

    If you write i++ it means you increment the variable i by one.
    This code:
    int i = 0;
    intArray[i++] = i;
    will store the value 1 at position 0 in the array. This is because i is incremented by 1 before its assigned.

  6. The Following User Says Thank You to Cornix For This Useful Post:

    sheng8 (August 16th, 2014)

  7. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Postfix in array[i++] seems effect my expecting result

    i learn the evaluate left-hand operand first rule, thank you

Similar Threads

  1. errormsg " expecting TRIPL_DOT, found myPorts "
    By Dominico Moon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 21st, 2014, 11:47 PM
  2. query to developed for 3 view effect
    By nishantapte in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 22nd, 2013, 03:16 AM
  3. result set array button
    By dread_arisawa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2010, 10:05 AM

Tags for this Thread