控制台程序
定义Point类:
public class Point {
// Create a point from its coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
} // Create a point from another point
public Point(Point point) {
x = point.x;
y = point.y;
} // Convert a point to a string
@Override
public String toString() {
return x+","+y;
} // Coordinates of the point
protected double x;
protected double y;
}
定义泛型类LinkedList<T>:
public class LinkedList<T> {
// Default constructor - creates an empty list
public LinkedList() {} // Constructor to create a list containing one object
public LinkedList(T 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(T[] items) {
if(items != null) {
// Add the items to the list
for(int i = 0; i < items.length; ++i) {
addItem(items[i]);
}
current = start;
}
} // Add an item object to the list
public void addItem(T item) {
ListItem newEnd = new ListItem(item); // Create a new ListItem
if(start == null) { // Is the list empty?
start = end = newEnd; // Yes, so new element is start and end
} else { // No, so append new element
end.next = newEnd; // Set next variable for old end
end = newEnd; // Store new item as end
}
}
// Get the first object in the list
public T getFirst() {
current = start;
return start == null ? null : start.item;
} // Get the next object in the list
public T getNext() {
if(current != null) {
current = current.next; // Get the reference to the next item
}
return current == null ? null : current.item;
} private ListItem start = null; // First ListItem in the list
private ListItem end = null; // Last ListItem in the list
private ListItem current = null; // The current item for iterating private class ListItem { // Constructor
public ListItem(T item) {
this.item = item; // Store the item
next = null; // Set next as end point
} // Return class name & object
@Override
public String toString() {
return "ListItem " + item ;
} ListItem next; // Refers to next item in the list
T item; // The item for this ListItem
}
}
使用LinkedList<T>泛型类的PolyLine类:
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
} // Add a point from a coordinate pair to the list
public void addPoint(double x, double y) {
polyline.addItem(new Point(x, y)); // Add the point to the list
} // String representation of a polyline
@Override
public String toString() {
StringBuffer str = new StringBuffer("Polyline:");
Point point = polyline.getFirst();
// Set the 1st point as start
while(point != null) {
str.append(" ("+ point+ ")"); // Append the current point
point = polyline.getNext(); // Make the next point current
}
return str.toString();
} private LinkedList<Point> polyline; // The linked list of points
}
程序入口:
public class TryGenericLinkedList {
public static void main(String[] args) {
// Create an array of coordinate pairs
double[][] coords = { {1, 1}, {1, 2}, { 2, 3},
{-3, 5}, {-5, 1}, {0, 0} }; // Create a polyline from the coordinates and display it
PolyLine polygon = new PolyLine(coords);
System.out.println(polygon);
// Add a point and display the polyline again
polygon.addPoint(10, 10);
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);
}
}
说明:
1、PolyLine类从LinkedList<T>泛型类中创建出LinkedList<Point>类型,从而实现了Point对象链表;
2、通过将Point作为类型变量T的参数传递到LinkedList<T>泛型类的定义中,可以生成相应的类类型。这个过程被称为类型擦除(Type erasure),因为出现类型变量T的所有位置都已经被Point替换。
3、在原始的泛型类定义中,类名之后出现的类型参数已经被移除,而且在类定义中出现T类型变量的位置都已被替换为Object类型。编译器之所以选择Object类型来替换类型变量,是因为Object类型是派生Point类型的最终超类。编译器选择的用于替换类型变量的类型是类型变量的最左边界。
4、设定类型参数的优势何在?毕竟使用Object类型作为参数能提供指向任意类型对象的引用。答案是:所提供的类型变量被编译器用来确保编译期间是类型安全的。在代码中使用LinkedList<Point>类型的对象时,编译器会检查使用它只是为了存储Point类型的对象,并且会将存储其它类型对象的任何企图都标记为错误。当调用LinkedList<Point>类型的对象的方法时,编译器会确保只提供Point类型的引用,其中原始的方法参数会被设定为类型参数。
5、定义和使用泛型类的本质内容其实很少。