2D Arrays
The goal of this lab period is to practice using 2D arrays in Java.
Tasks
- Setup
Create a working directory for today's lab. Then start a Java file called Grid.java and make a public class called Grid.
- Explore Command-line Parameters
Create a main function in the Grid class. The args parameter of the main() function contains strings from the command line. Use a for loop to print out all of the elements of the args array. Then run your Grid class a few different times with additional strings on the command line. For example, try the following:
$ java Grid 42 96 81 hut hut
Note what your program displays. Change around the command-line arguments and see the effect. What data type are the command line parameters?
- Usage Statements
Many programs you will write will require command line arguments in order to work properly. To be kind to other people using your program you should always check if the user provided enough arguments and print out a usage statement if they don't. A usage statement prints out an example of how to run the program, a description of what it does, and what the arguments mean. For example, if you type
java
on the command line with no arguments, it prints out the following.Usage: java [options]
[args...] (to execute a class) or java [options] -jar [args...] (to execute a jar file) or java [options] -m [/ ] [args...] java [options] --module [/ ] [args...] (to execute the main class in a module) or java [options] [args] (to execute a single source-file program) ... In your main function, before doing anything else, test if the user provided at least one command line argument. If they did not, then print out a usage statement and return. Test your function with no arguments and with one or more arguments.
- Creating a 2-D Array
Create a 2D array of type String. I'll call it grid.
String[][] grid;
Start by allocating the 2D array in a single new statement, giving it 3 rows and 5 columns.
grid = new String[3][5];
If you try to print out
grid
, what happens? Presumably, you'll get something likeString@somegarbage
. Unfortunately, Java Arrays don't natively implement a 'pretty' toString, so you have to loop through Arrays yourself to print thing out nicely. As a follow-up, if you try to print outgrid[0][0]
, what gets printed out?Now let's try the alternative of allocating the row size and column size separately:
grid = new String[3][]; for(int i = 0; i < 3; i++){ grid[i] = new String[i+3]; }
Notice that by this method, the rows of
grid
are actually different lengths. However, be a little careful with this method: if you try to print outgrid[0][0]
between the instantiation ofgrid
and the start of the for loop, what happens?To fix the null entries of
grid
, regardless of which way you instantiated the object, you have to go through every location in the grid and create the String object for it. Do that now and give each location in the grid a random character.Hint: run the following code and note what gets printed. Check out this ascii table for more details.
char letterA = (char) 65; System.out.println(letterA);
+ (More Detail)
When working with 2-D arrays we often use nested for loops:
for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { /* access the elements of the array using i and j as index variables */ } }
Remember that you have to use different names for the nested loop variables. For almost all 2-D arrays, you want to loop over rows in the outer loop and columns in the inner loop. The reason is that most 2-D arrays are stored in row-major order in memory, so looping over rows and then columns accesses memory in linear order, which is faster than jumping around.
When creating a 2-D array, you have to first allocate the 2-D grid of String references, then use a nested loop, as above, to allocate each String element grid[i][j] individually. Assign to each grid[i][j] a new String by adding a random char to a blank string ("").
- Print the Contents of the 2-D Array
Print out the contents of the 2D array as a 2D grid using a nested for loop. Make it look nice. As part of the print process, print out the number of rows and columns in the grid, querying the array for its length. Hint: Test using the .length field of arrays to retrieve both the number of rows and the number of columns, try thinking of a 2D array as an "array of arrays."
+ (More Detail)
If you have a 2-D array such as
String[][] grid;
, you can get the number of rows by using the expressiongrid.length
. You can get the number of columns of row i by usinggrid[i].length
. You can use these in the for loop conditions so the code adapts to the number of rows and columns in the 2-D array, whatever size it is.The function
System.out.print
does not put a newline character after the string to print. The functionSystem.out.println()
does put a newline character. When printing the contents of a 2-D array, it's nice to keep all the elements of a row on the same line.If you want control over how Java formats numbers, especially floating point numers, look at
System.out.printf
and look up printf formatting codes. - Checking if two matrices are equal
Try running the following code:
int[][] arr1 = new int[2][2]; int[][] arr2 = new int[2][2]; int[][] arr3; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ arr1[i][j] = i+j; arr2[i][j] = i+j; } } arr3 = arr1; System.out.println(arr1 == arr2); System.out.println(arr1 == arr3);
Obviously, all of these arrays are the same, but Java says that the first two are not equal. This is because the==
operator tests memory location rather than if the two objects are actually the same. Sincearr3
was instantiated by referencingarr1
's literal reference,arr1 == arr3
according to Java.Create a method that checks if two grids have the same contents with the following declaration:
public static boolean gridEquals(int[][] arr1, int[][] arr2)
. Create some good tests to confirm your code works. Your method should work even if your inputs aren't the same size! - Rotating a matrix
For the final task, create a function with the following declaration:
public static int[][] rotate(int[][] arr)
. This function should rotate a grid by 90 degrees clockwise. For example, the grid1 2 3 4 5 6
would return5 3 1 6 4 2
After creating it, use your abovegridEquals
method to create some tests that it's working. Note that this method should still work even if the grid isn't a square; however, you can and should assume the grid is rectangular.
When you are done with the lab, go ahead and get started on the current project.