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

Thread: Jagged array loop question

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

    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:

    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:

    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:
    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Jagged array loop question

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65055-jagged-array-loop-question.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. While Loop quick question
    By loui345 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 11:46 PM
  2. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  3. [SOLVED] VERY basic question - if loop
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 12th, 2011, 06:34 PM
  4. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM
  5. [HELP] Iterating through a jagged edged array
    By AlexBeer in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2010, 06:33 AM