|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
Review solution:
How 2-D arrays are represented. Processing an entire array, by processing one row at a time.
Compare: How to represent objects in
Super Mario Galaxy?
What classes involved? What fields? What methods?
Note that they have an 'angry' mode, which eventually stops.
How is that implemented?
hw10—gRUe: what classes involved? What fields? What methods?
We can have arrays of any type, in Java.
We've seen, array-of-double (
For example: This might be a topographical map of elevations (with a butte near the center-north):
+------+------+------+------+------+------+------+ | 22.2 | 17.7 | 83.3 | 80.0 | 63.3 | 15.5 | 10.0 | +------+------+------+------+------+------+------+ | 19.9 | 11.1 | 88.8 | 72.2 | 42.2 | 14.4 | 14.4 | +------+------+------+------+------+------+------+ | 8.8 | 10.0 | 13.3 | 52.2 | 12.2 | 13.3 | 12.2 | +------+------+------+------+------+------+------+ | 10.0 | 11.1 | 15.5 | 18.8 | 13.3 | 15.5 | 10.0 | +------+------+------+------+------+------+------+
double[][] elevations; elevations = new double[4][7]; // #rows, then #columns. |
double[] 0 1 2 3 4 5 6 +------+------+------+------+------+------+------+ /----->| 22.2 | 17.7 | 83.3 | 80.0 | 63.3 | 15.5 | 10.0 | | +------+------+------+------+------+------+------+ +------+ | elevations| *---|--\ |double[] 0 1 2 3 4 5 6 +------+ | double[][] | +------+------+------+------+------+------+------+ \---->+-----+ | /-->| 19.9 | 11.1 | 88.8 | 72.2 | 42.2 | 14.4 | 14.4 | 0| *---|----/ | +------+------+------+------+------+------+------+ +-----+ | 1| *---|-------/double[] 0 1 2 3 4 5 6 +-----+ +------+------+------+------+------+------+------+ 2| *---|---------->| 8.8 | 10.0 | 13.3 | 52.2 | 12.2 | 13.3 | 12.2 | +-----+ +------+------+------+------+------+------+------+ 3| *---|-----\ +-----+ |double[] 0 1 2 3 4 5 6 | +------+------+------+------+------+------+------+ \---->| 10.0 | 11.1 | 15.5 | 18.8 | 13.3 | 15.5 | 10.0 | +------+------+------+------+------+------+------+(Even though we'll think of
/** Fill one row of z 2-D array, * so that location (rowNum,j) containing rowNum*j. * @param data The array to initialize. * @param rowNum Which row to initialize the contents of. */ public static void fillOneRow( double[][] data, int rowNum ) { for (int c=0; c < data[rowNum].length; ++c) { data[rowNum][c] = rowNum*c; } } /** Fill an entire 2-D array, so that location (i,j) containing i*j. * @param data The array to fill. */ public static void fillArray( double[][] data ) { for (int r=0; r < data.length; ++r) { fillOneRow(data, r); } } /** An example of declaring, initializing, and using a 2-D array. */ public static void main( String[] args ) { double[][] elevations = new double[4][7]; fillArray( elevations ); System.out.println( "The array is: " + java.util.Arrays.deepToString( elevations ) ); // Optional: // If you copy the provided class Pict.java into your project (as a separate file), // then you can call: Pict.displayPixels(elevations); // We'll explain this method more, below. } |
Note that if we wanted to, we could certainly collapse the above steps:
// ALTERNATELY, most books will "in-line" this to a *nested* loop: for ( int row=0; row < elevations.length; ++row) { for ( int col=0; col < elevations[row].length; ++col ) { elevations[row][col] = row*col; } } /* Compare: nested loops, vs separate methods: * - nested loops shorter, but more to think about all at once * - separate methods: easier to get right. * - separate methods: ** YOU CAN TEST THE METHOD INDIVIDUALLY! ** * This makes it *much* easier to track down bugs. * (I have seen many a student bang their head against a wall for hours * trying to debug something with nested loops, where separate * functions with a couple tests each would have elucidated * the problem quite quickly.) */ |
Note that Java has some nice syntax for initializing arrays :
double[] rowZero = { 22.2, 17.7, 83.3, 80.0, 63.3, 15.5, 10.0 }; |
These array-literals naturally extend to arrays-of-arrays, so we can write:
double[][] elevations = { { 22.2, 17.7, 83.3, 80.0, 63.3, 15.5, 10.0 } { 19.9, 11.1, 88.8, 72.2, 42.2, 14.4, 14.4 } { 8.8, 10.0, 13.3, 52.2, 12.2, 13.3, 12.2 } { 10.0, 11.1, 15.5, 80.0, 13.3, 15.5, 10.0 } }; |
(Sadly, these aren't quite “array literals”,
in the way that
mentioning .)
The
Group Task
Write a method which takes any 2-D array of
To add up all the values in the array,
you can either use a helper method (
something like
Your Task
Write a method which takes any 2-D array of
You can either use a helper method
(
Your Task
Write a method
You can either use a helper method
(something like
1 Actually, BlueJ let's you type this in its interactive call-a-method dialog box. ↩
2 Hey, for once integer division is what we want, since we're storing the result back into an int. Note that any number in [0,256), when average with 256, is still in [0,256). That's because (255+256)/2 is 255, by integer division. This will be important below. ↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Dec.07 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |