1. Learn
  2. /
  3. Courses
  4. /
  5. Data Types and Exceptions in Java

Connected

Exercise

Autoboxing and foreach

In this exercise, you will explore autoboxing and looping through a List - specifically a LinkedList of prices. You will add some double values to the LinkedList, allowing autoboxing to convert them to Double, and use the “foreach” loop to calculate the average price of the items in the LinkedList that has been imported for you.

Instructions

100 XP
  • Construct a new LinkedList of Doubles and set the prices variable to it.
  • Add a new price (a double) 9.65 to the end of the prices list. Autoboxing will automatically convert the double to a Double before adding it.
  • Add a new price (a double) 1.35 to the beginning of the prices list. Autoboxing will automatically convert the double to a Double before adding it.
  • Use a "foreach" loop to iterate through and sum all prices.