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)?
Re: Jagged array loop question