Results 1 to 3 of 3
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default incrementing array elements

    OK this one is more me asking someone to explain this to me because it doesn't seem intuitive at all:

    I encountered some code today that looked sort of like this:
    Java Code:
                    int[] array = new int[5];
    		int pos = 0;
    		int value = 1;
    		
    		
    		for (int i = 0; i < array.length; i++)
    		{
    			[B]array[pos++] = value;[/B]
    			value++;
    		}
    		for (int i = 0; i < array.length; i++)
    			System.out.println("element " + i + " with value="+array[i]);
    The bold section of code is that part I didn't know about. Why is it that 'pos' is incremented only after the assignment is done?

    It seems to me like it should be the other way around. That 'pos' should increment first and then the assignment should happen. This would mean that in the first iteration pos would increment to 1 and the first value would instead be assigned to element [1] in the array not [0].

    Why does it increment afterwards?
    Last edited by porchrat; 10-06-2010 at 01:57 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    That is called the post increment operator, which means the original is value is evaluated in that position. As soon as it is evaluated, the value is then incremented.

    If you want it incremeneted first then you need pre increment operator, i.e.
    Java Code:
    ++pos
    rather than
    Java Code:
    pos++
    .

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    That is called the post increment operator, which means the original is value is evaluated in that position. As soon as it is evaluated, the value is then incremented.

    If you want it incremeneted first then you need pre increment operator, i.e.
    Java Code:
    ++pos
    rather than
    Java Code:
    pos++
    .
    Oh that makes sense. Duh.

    Sorry that was a rather stupid question.

    I normally don't run into that because I always increment on a separate line :p

    Thanks for the explanation.

Similar Threads

  1. incrementing elements in an array
    By JavaNubb in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 03:15 AM
  2. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 08:36 AM
  3. How array elements gets default value
    By Veangat in forum New To Java
    Replies: 1
    Last Post: 03-07-2010, 01:29 PM
  4. Looping Array Elements
    By enzyme in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 04:35 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 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
  •