Java Reference
In-Depth Information
cludespacesbetweentheangledbrackets,yourcodestillcompiles,butitisrecommendedthat
you don ' t do this.
The argument that you supply to a generic type could also be a type that you define using a generic type.
Look at this example:
LinkedList<LinkedList<String>> texts = new LinkedList<>();
Here you have created an object that implements a linked list in which you can store objects that are
themselves linked lists of type LinkedList<String> . Thus, you have defined a linked list of linked lists!
To apply the new generic LinkedList<> type that you have defined in a working context, let's repeat the
TryPolyLine example from Chapter 6 using a type that is generated from the LinkedList<> type.
TRY IT OUT: Using a Generic Linked List Type
First you need a modified version of the PolyLine class that uses the LinkedList<T> generic type:
public class PolyLine {
// Construct a polyline from an array of coordinate pairs
public PolyLine(double[][] coords) {
Point[] points = new Point[coords.length]; // Array to hold
points
// Create points from the coordinates
for(int i = 0 ; i < coords.length ; ++i) {
points[i] = new Point(coords[i][0], coords[i][1]);
}
// Create the polyline from the array of points
polyline = new LinkedList<>(points);
// Create list of
Point objects
}
// Construct a polyline from an array of points
public PolyLine(Point[] points) {
polyline = new LinkedList<>(points);
// Create list of
Point objects
}
// Add a Point object to the list
public void addPoint(Point point) {
polyline.addItem(point); // Add the point to
the list
}
Search WWH ::




Custom Search