CS 231: Lab #2

Title image Spring 2020

2D Arrays

The goal of this lab period is to practice using 2D arrays in Java.


  1. Setup

    Create a working directory for today's lab. Then start a Java file called Grid.java and make a public class called Grid.

  2. 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?

  3. 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 cp on the command line with no arguments, it prints out the following.

    usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
           cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

    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.

  4. Converting Strings to Integers

    Declare two int variables in the main function, I'll call them yogi and booboo. Convert arguments 0 and 1 of the command line strings into integers and assign them to yogi and booboo. Given a string s, you can make the conversion using the Integer method parseInt():


  5. Creating a 2-D Array

    Create a 2D array of type String. I'll call it ranger.

    String[][] ranger;

    Start by allocating the 2D array in a single new statement, giving it yogi and booboo as the row and column sizes. Then try the alternative of allocating the row size and column size separately. Is there any value in the second method?

    Allocating a 2D grid of String references does not create any String objects; it creates only references/pointers to String objects. To create the actual objects, 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: the expression 'a' means the ascii value of the character 'a'.

    + (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 ranger[i][j] individually. Assign to each ranger[i][j] a new String() with its initial value in the String constructor.

  6. 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[][] ranger;, you can get the number of rows by using the expression ranger.length. You can get the number of columns of row i by using ranger[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 function System.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.

When you are done with the lab, go ahead and get started on the current project.