
Title:
2D Arrays Part 3  Intro to Java Programming

Description:

To visit all elements in a TwoD array, you want to loop over the rows and

columns. Let's first loop over the rows. So we have a row index i that assumes

to values zero, one, and two in this example. Similarly, we'll have a column

index j, then in this example, we'll go from zero to one. If we had more

columns, of course it would go further. When you have a row of column index,

then we can access the element at the ith row, and the jth column. So what

you see over here. As the general expression for an element at an arbitrary row

and arbitrary column. In this case, we just print it and we use printf. So that

the print out lines up nicely. So we would now print this element and print

that element, that would finish the innerloop. Then the outer loop would pick

the next row, we print these two, and then the outer loop picks the last row.

And we print those two. Now, of course, we want the numbers to line up nicely,

so after printing each row, we want to print a new line. Notice that this

statement is contained in the outer loop, because it happens once per row. But

its not in the inner loop because we don't want a new line after every of the

element. Now, lets look at the missing balance here. Of course, in this simple

example I could just say I should be less than three, J should be less than

two, but in general, someone might just hand you a twodimensional array and

you should ask it how big it is. Just like with a onedimensional array, you

just use the length field to find out how big an array is. You can get the

number of rows from a twodimensional array by asking it arrayname.length. And

the reason for this is that a twodimensional array is actually an array of

onedimensional arrays. So prices, which looks like this nice tabular

arrangement, really is an array of three arrays, one for each row. And so the

number of rows is given by that length. Now, we need to look at how many

columns we have. Here you have a row. And the length of that row is the number

of columns. So in general, you should remember that, for any twodimensional

array, you get the number of rows with this expression, the number of columns

with that expression. Now, let's move on to doing something more interesting

with twodimensional arrays and gas prices.