Java Reference
In-Depth Information
Double that encapsulates it. Thus, the creation of the appropriate wrapper class object is taken care of auto-
matically, so you can use the linked list object as though it stored values of the primitive type.
Let's see if the linked list type can really take the heat.
TRY IT OUT: Autoboxing with Generic Types
In this example you create an instance of the LinkedList<T> generic type and use it to store random
temperature values of type double :
public class TryAutoboxing {
public static void main(String[] args) {
LinkedList<Double> temperatures = new LinkedList<>();
// Insert 6 temperature values 0 to 25 degress Centigrade
for(int i = 0 ; i < 6 ; ++i) {
temperatures.addItem(25.0*Math.random());
}
Double value = temperatures.getFirst();
while(value != null) {
System.out.printf("%.2f degrees Fahrenheit%n",
toFahrenheit(value));
value=temperatures.getNext();
}
}
// Convert Centigrade to Fahrenheit
public static double toFahrenheit(double temperature) {
return 1.8*temperature+32.0;
}
}
Directory "TryAutoboxing"
Of course, you need to copy the LinkedList.java source file to the directory for this example. This
program outputs something similar to the following:
72.88 degrees Fahrenheit
32.80 degrees Fahrenheit
38.36 degrees Fahrenheit
65.76 degrees Fahrenheit
65.92 degrees Fahrenheit
67.56 degrees Fahrenheit
How It Works
Search WWH ::




Custom Search