Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Jagged array loop question

  1. #1
    Dankaru is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Jagged array loop question

    Okay I am examining a jagged array loop example from a book:

    This code creates and initializes a jagged array of integers:

    Java Code:
    int number = 0;
    int[][] pyramid = new int[4][];
    for (int i = 0; i < pyramid.length; i++)
    {
        pyramid[i] = new int[i+1];
        for (int j = 0; j < pyramid[i].length; j++)
        {
            pyramid[i][j] = number++;
        }
    }
    This code prints it:

    Java Code:
    for (int i = 0; i < pyramid.length; i++)
    {
        for (int j = 0; j < pyramid[i].length; j++)
        {
            System.out.print(pyramid[i][j] + "  ");
        }
        System.out.print("\n");
    }
    They print:
    0
    1 2
    3 4 5
    6 7 8 9

    It works, and i can see why and everything. My question concerns this line:
    Java Code:
    pyramid[i][j] = number++;
    Why does it print 0 first?
    Shouldn't pyramid[0][0] = 1? Doesn't the loop run through the first time and set pyramid[0][0] to numbers++ (numbers = 0; numbers = numbers + 1)?

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Jagged array loop question

    numbers++ is quick of saying
    Java Code:
    numbers += 1;
    OR
    numbers = numbers + 1;
    The reason why it's 0 the first time is because there's 2 ways of using the ++,
    Java Code:
    numbers++ 
    AND
    ++numbers
    the difference between the two is that numbers++ first does whatever it has to do and then it increments your number by one, ++numbers first increments the number by one and then it does whatever has to happen.

  3. #3
    Dankaru is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Jagged array loop question

    I had a feeling it was something like that, thanks!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Jagged array loop question

    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 10:39 PM
  2. Enhanced For Loop Array List Reference Question
    By smande3 in forum New To Java
    Replies: 1
    Last Post: 06-30-2012, 04:42 AM
  3. Replies: 5
    Last Post: 03-29-2012, 07:22 PM
  4. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  5. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 03:26 AM

Posting Permissions

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