# Jagged array loop question

• 11-14-2012, 02:24 PM
Dankaru
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:

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:

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:
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)?
• 11-14-2012, 02:35 PM
JBelg
Re: Jagged array loop question
numbers++ is quick of saying
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 ++,
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.
• 11-14-2012, 02:45 PM
Dankaru
Re: Jagged array loop question
I had a feeling it was something like that, thanks!
• 11-14-2012, 03:21 PM
KevinWorkman
Re: Jagged array loop question