CSU Banner

CS 163/164, 🀨 Fall 2019 🀨

Lab 15 - More ArrayList Practice

Thursday, Nov. 7th or Friday, Nov. 8th


Overview

This lab will continue our exploration of the ArrayList data structure. Specifically, we will cover the topics below: Remember from the previous lab that in order to use an ArrayList, you first have to import the class:

import java.util.ArrayList;

You can then create a new ArrayList object:

ArrayList<Object> listTest = new ArrayList<Object>();

The Java API has a list of all the methods provided by an ArrayList. See: Java ArrayList API.

Assignment Setup

To begin this lab create a new Java project in eclipse called "ArrayListPractice". Within your new project, download the file below and put it in the src folder.

Your Task

In the previous lab we learned about why ArrayLists are useful and started working with them. In this lab we will use an ArrayList to store String objects in order to learn what we can and can not do with them. Follow the steps outlined below to complete this lab. Your lab instructor will explain to you why some things work and why others do not.

  1. Create an ArrayList object that holds strings, initialize the capacity to size 10.
  2. Add a String for each of the first five planets in the solarSystem ArrayList.
  3. Look at the example of the for-each loop in the provided code, run your code and observe the output.
  4. At index 3 insert the String containing the name of our planet's only natural satellite.
  5. Use a for-each loop like in the provided example to print the contents of the solarSystem ArrayList.
  6. At the appropriate indices insert the name of our local star and the collective group of celestial objects between Mars and Jupiter.
  7. The provided code tries to print the contents of the solarSystem ArrayList at index 8. Run the code and observe that it fails. Why does it fail, didn't you initialize the size of your ArrayList to 10? Answer in the comments of ArrayListPractice.java.
  8. Add the remaining planets that come after Jupiter to your ArrayList, go ahead and add Pluto as well.
  9. What is the size of your ArrayList now? Print a line that uses the .size() method on your ArrayList.
  10. How is the size bigger then the initial capacity you provided for the solarSystem ArrayList you provided? Explain what happened in the comments of ArrayListPractice.java.
  11. Observe in the provided code how another ArrayList, solarSystem2 is created as a "copy" of the first solarSystem ArrayList.
  12. Use a for-each loop again to print the contents of solarSystem2 to see if it is the same as the solarSystem ArrayList. Answer whether it is in the comments of ArrayListPractice.java.
  13. Remove Pluto from the solarSyatem ArrayList. It's not really a planet anyway, right?
  14. Use the for-each loop once again to print the contents of solarSystem2. It changed, why? Answer in the comments of ArrayListPractice.java.
  15. Remove the first seven elements in the solarSystem ArrayList with a standard for loop.
  16. Make another ArrayList of Strings called solarSystem3 and initialize it with strings to match the remaining contents of the original solarSystem ArrayList as detailed in the prvious step.
  17. At this point the two ArrayLists solarSystem and SolarSystem3 should be the same. Check their equality with the == operator and the .equals() method. Your TA will explain the difference to you in detail.


Show your ArrayListPractice.java to the TA for grading.