Lesson 18 – Two-dimensional Data

Java Concepts

2D Arrays, files

Teacher Materials

Get Access

You have already used arrays and ArrayLists to store lists of Finch data. Using these tools, measuring multiple things requires multiple data structures. For example, suppose you want to collect distance data as a function of time. You could create one data structure for time and one for distance. However, if these data are connected, it would make more sense to record them in a single data structure with two dimensions. In this lesson, you will learn to use two-dimensional arrays to record Finch data.

You can think about a two-dimensional array as having rows and columns. For example, this table is an array with five rows and two columns.

To declare a two-dimensional array in Java, you first need to choose the number of rows and columns that you want the array to have. Declaring the array is similar to declaring a one-dimensional array, except that you use two sets of square brackets. The number inside the first set indicates the number of rows that you want, and the number in the second set indicates the number of columns.

To access an element in the array, you also use two sets of square brackets. The number in the first set of square brackets indicates the index of the row, and the number in the second set indicates the index of the column. Just like for one-dimensional arrays, each index starts at 0. For example, this code records time in the first column and distance in the second. Each row represents a time measurement and the distance measured at that time.

Exercise 1

Create a class named DataFinch2D that extends the Finch class. You will build on this class throughout this lesson. Start by adding the method described below. Don’t forget to create a test class with a main() method to test DataFinch2D!

  • measureTimeAndDistance() should ask the user for a length of time in seconds. It should then record the time and the distance measured by the Finch every 500 ms for the chosen time period.
  • The method should return an array containing the measured data.

Exercise 2

Add a second method named measureAll() to your DataFinch2D class.

  • This method should record time, distance, both light sensor values, both line sensors values, and the compass value. Hint: You will need more than two columns for this array.
  • The method should ask the user for a length of time in seconds. It should then record all seven values once per second for the chosen time period.
  • The method should return an array containing the measured data.

Saving Data in Files

You can record a lot of data with your Finch, but it is not stored permanently. The data structures containing the data disappear when the program ends. To store this data more permanently, you can write it to a file. For example, you can write the first row of data2DArray to a file named “data.txt” as shown below. The try-catch structure is a control structure used in Java to execute code that might fail. If the code inside the try fails, the code inside the catch is executed. In this case, an error is printed if our attempt to write to a file fails.

To save information to a file, you first create a FileWriter object. The constructor for this object takes the name of the file. Then the FileWriter write() method can be used to place strings in the file.

Writing to a file is very similar to using System.out.println() to print text on the screen. For example, you can use the + operator to combine strings.

Exercise 3

Add a method named writeDataToFile() to the DataFinch2D class. This method should accept a two-dimensional array of type double and write the first column of that array to a file. Make sure you know where your computer is saving the file. In Eclipse, it is in the folder that contains the src folder in which you store your code.

Exercise 4

Modify your method from Exercise 3 so that for each row, it records the value in each column on that row. These data values should be separated by commas. All the data from a single row should be on the same line in the file. Try importing your data file into a spreadsheet program to graph your data!