# Thread: Jagged array loop question

1. Member Join Date
Nov 2012
Posts
4
Rep Power
0

## 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[];
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 = 1? Doesn't the loop run through the first time and set pyramid to numbers++ (numbers = 0; numbers = numbers + 1)?  Reply With Quote

2. Member Join Date
Oct 2012
Posts
36
Rep Power
0

## 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.  Reply With Quote

3. Member Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: Jagged array loop question

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

4. ## Re: Jagged array loop question  Reply With Quote

#### Posting Permissions

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