Lesson 17 – Storing Data in ArrayList

Java Concepts

ArrayList, selection sort

Teacher Materials

Get Access

In Lesson 11, you used arrays to store data that you collected with the Finch sensors. However, an array has a fixed size. That means that you had to choose in advance how many data points you wanted to store. An ArrayList, on the other hand, is a data structure that does not have a fixed size. You can increase or decrease its size as you collect data.

You can create an empty ArrayList of integers by importing java.util.Array list and then declaring an empty list as shown below. Notice that you cannot create ArrayLists of primitive data types like int, float, and double. Instead, you must use Integer, Float, and Double. These are classes that correspond to the primitive data types. 

Once you have created an ArrayList, you can add data to the end of it using the add() function. For example, this code adds the value of the distance sensor to the ArrayList every 0.1 s until the user places the Finch beak up. It then prints out the number of data points that were recorded.

Exercise 1

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

  • collectDistanceData() should use the Finch distance sensor to measure the distance to the closest object every 0.1 s until the Finch is placed in the beak up position. It should return the data as an ArrayList.
  • collectLightData() should use the Finch light sensors to measure the ambient light every 0.1 s until the Finch is placed in the beak up position. It should return the data as an ArrayList. At each time point, the values of the left and right light sensors should be averaged and rounded to the nearest integer. 

You can traverse an ArrayList by accessing each element using an index, just as you would with an array. For example, this code prints every element in an ArrayList named data. Instead of using square brackets, you access an element in an ArrayList by passing the index of the element to the get() method, which returns the element at that index.

However, you can also use an enhanced for loop to traverse an ArrayList. The syntax for this for loop is a little simpler. Within the parentheses following the keyword for, you declare a variable of the appropriate type and use a colon to separate it from the name of the ArrayList. In the code below, the variable value will take on each of the elements in the ArrayList data. The first time through the for loop, value will be equal to the first value in the ArrayList data, the second time through value will be equal to the second value, etc. The result of this for loop will be the same as the one above, but the enhanced for loop does not use the indices.

Exercise 2

Use the enhanced for loop to expand your DataFinch class by adding two more methods:

  • findMaximum() accepts an ArrayList<Integer> as a parameter and returns the maximum value in this ArrayList.
  • findMinimum() accepts an ArrayList<Integer> as a parameter and returns the minimum value in this ArrayList.

Selection Sort

To make your DataFinch class more useful, it might be helpful to have the option to sort the data that the Finch collects. One way to do this is selection sort. To use selection sort, you start at the first element in an ArrayList (or other data structure). You look at all the elements of the ArrayList except the first element and find the minimum. If that minimum is less than the first element, you swap the two elements. Then you move on to the second element in the ArrayList and compare it to the minimum of all the elements in the ArrayList except the first and the second. As you continue this process, you gradually order the elements in the list from the least to the greatest. More information on selection sort, as well as a nice visualization, is available here.

Exercise 3

To begin implementing selection sort for your DataFinch class, create a method named findMinimumWithIndex() that finds the minimum element and its index for a portion of an ArrayList.

  • The method should accept two parameters, an ArrayList of integers (data) and an integer that represents the index where the search should start (startIndex).
  • The method should search through data from startIndex to the end and find both the minimum element in that portion of the ArrayList and its index.
  • The function should return an array of two values, with the first value being the minimum element and the second being the index of that element.

Exercise 4

Use your method from Exercise 3 to create a method named selectionSort().

  • This method should accept an ArrayList of data and return an ArrayList of the data sorted from least to greatest.
  • For each element in the ArrayList, compare that element to the minimum of the rest of the array. If the minimum is less than the current element, swap the two.
Back to Top