Java Reference
In-Depth Information
System.out.println(polygon);
// Create Point objects from the coordinate array
Point[] points = new Point[coords.length];
for(int i = 0 ; i < points.length ; ++i) {
points[i] = new Point(coords[i][0],coords[i][1]);
}
// Use the points to create a new polyline and display it
PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);
}
}
Directory "TryGenericLinkedList"
Apart from the class name, this is the same as the
TryPolyLine
class that you created in Chapter 6. Com-
piling this program results in five
.class
files. Two are the result of compiling
LinkedList.java
. The
source for the generic type compiles into
LinkedList.class
plus the
LinkedList$ListItem.class
corresponding to the inner class. When you execute this example, it produces the same output as the ex-
ample in Chapter 6.
How It Works
The
PolyLine
class creates the
LinkedList<Point>
type from the
LinkedList<T>
generic type that
implements a linked list of
Point
objects because of this statement:
private LinkedList<Point> polyline; // The linked list
of points
The class type that results from this is produced by passing
Point
as the argument for the type variable
T
in the
LinkedList<T>
generic type definition. This process is described as
type erasure
because all
occurrences of the type variable
T
are eliminated, so you end up with a notional class with the following
definition:
public class LinkedList {
// Default constructor - creates an empty list
public LinkedList() {}
// Constructor to create a list containing one object
public LinkedList(Object item) {
if(item != null) {
current=end=start=new ListItem(item); // item is the start and
end
}
}
// Construct a linked list from an array of objects
public LinkedList(Object[] items) {
if(items != null) {
// Add the items to the list
for(int i = 0 ; i < items.length ; ++i) {
addItem(items[i]);