|
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
We will:
We can have arrays of any type, in Java. We've seen, array-of-double (double[]) and array-of-string (String[]), for example. But we might also have array-of-array-of-double (that is, double[][]). We call this a two-dimensional array.
For example: This might be a topographical map of elevations (with a butte near the center-north):
+----+----+----+----+----+----+----+ | 22 | 17 | 83 | 80 | 63 | 15 | 10 | +----+----+----+----+----+----+----+ | 19 | 11 | 88 | 72 | 42 | 14 | 14 | +----+----+----+----+----+----+----+ | 8 | 10 | 13 | 52 | 12 | 13 | 12 | +----+----+----+----+----+----+----+ | 10 | 11 | 15 | 18 | 13 | 15 | 10 | +----+----+----+----+----+----+----+
int[][] elevations; elevations = new int[4][7]; // #rows, then #columns. |
int[] 0 1 2 3 4 5 6 +----+----+----+----+----+----+----+ /----->| 22 | 17 | 83 | 80 | 63 | 15 | 10 | | +----+----+----+----+----+----+----+ +------+ | elevations| *---|--\ | int[] 0 1 2 3 4 5 6 +------+ | int[][] | +----+----+----+----+----+----+----+ \---->+-----+ | /-->| 19 | 11 | 88 | 72 | 42 | 14 | 14 | 0| *---|----/ | +----+----+----+----+----+----+----+ +-----+ | 1| *---|-------/int[] 0 1 2 3 4 5 6 +-----+ +----+----+----+----+----+----+----+ 2| *---|---------->| 8 | 10 | 13 | 52 | 12 | 13 | 12 | +-----+ +----+----+----+----+----+----+----+ 3| *---|-----\ +-----+ | int[] 0 1 2 3 4 5 6 | +----+----+----+----+----+----+----+ \---->| 10 | 11 | 15 | 18 | 13 | 15 | 10 | +----+----+----+----+----+----+----+(Even though we'll think of elevations as a grid, the diagram reveals that it is really an array-of-arrays. Some languages do have “true” 2-D arrays, but array-of-array simply uses pre-existing concepts.)
public static void main( String[] args ) { int[][] elevations = new int[4][7]; for ( int row=0; row < elevations.length; ++row) { for ( int col=0; col < elevations[row].length; ++col ) { elevations[row][col] = row*col; } } 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.drawPixels(elevations); // We'll explain this method more, below. } |
Note that Java has some nice syntax for initializing arrays :
int[] rowZero = { 22, 17, 83, 80, 63, 15, 10 }; |
These array-literals naturally extend to arrays-of-arrays, so we can write:
int[][] elevations = { { 22, 17, 83, 80, 63, 15, 10 } { 19, 11, 88, 72, 42, 14, 14 } { 8, 10, 13, 52, 12, 13, 12 } { 10, 11, 15, 80, 13, 15, 10 } }; |
(These are called “array literals”, just like mentioning 17 in your program is an int literal, and mentioning "hello" is a string literal.)
The textbook gives an example 2-D arrays to show a tic-tac-toe board in §7.6.
Group Task
Write a method which takes any 2-D array of ints,
and calculates the average value
(that is, it adds them all up, and divides by the length×width).
To add up all the values in the array,
you can either use a helper method (
something like addOneRow( int[][] nums, int rowNum)),
or you can use a nested loop.
Your Task
Write a method which takes any 2-D array of ints,
and finds the maximum value.
You can either use a helper method
(findMaxInRow( int[][] nums, int rowNum)),
or you can use a nested loop.
Your Task
Write a method moveEachToward(int[][] nums, int target)
which takes any 2-D array of ints,
and replaces each number n with1
(n+target)/2.
That is,
moveEachHalfwayTo(0)
ends up halving each item in the array,
and
moveEachHalfwayTo(256)
makes each item “twice as close” to 256.
You can either use a helper method
(something like
moveEachInRowToward( int[][] nums, int target, int rowNum)),
or you can use a nested loop.
We have provided a class (documentation and source code). with two helpful static methods:
Pict.displayPixels takes in any two-dimensional array of ints in [0,256), and creates a window showing that array as a picture.
// int pixels as a literal array, then displayed: int[][] data1 = { {30, 30, 30, 128}, {40, 200, 40, 128}, {50, 90, 50, 128} }; displayPixels( data1 ); |
Task: Write a method which creates a 500x500 array, then initializes location i,j is initialized to i % 256. What do you think this will look like, as a picture? Display it, and find out.
Task: Now, tweak the above method to try different formulas. (For each one, you're encouraged to predict the result first.)
(i*j)*256/(width*height) |
if ((i*j)%59 == 0) set location i,j to 0 else set location i,j to 255. |
if ((i*j)%60 == 0) set location i,j to 0 else set location i,j to 255. |
Choose one effect from the following list, and implement it! To figure out what you should do, work a small test case by hand (say, the 3x4 array data1 shown above).
Flip the picture left-to-right.
To do this, you need to think:
where should pixel 0,0 end up?
What about pixel 2,0? or 2,3?
See if you can figure out the general formula for pixel r,c.
(Your formula should involve the array's width and height, in general.)
Recall how our selectionSort code from lecture moved items around inside the array (using one variable tmp when swapping).
There are also plenty of other photo effects you could do, instead of the above.
1 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
textbook—tutor/PIs—java.lang docs—java.util docs
©2009, Ian Barland, Radford University Last modified 2009.May.06 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |