控制台程序

定义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、定义和使用泛型类的本质内容其实很少。

Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)的更多相关文章

  1. Java基础之多态和泛型浅析

    Java基础之多态和泛型浅析 一.前言: 楼主看了许多资料后,算是对多态和泛型有了一些浅显的理解,这里做一简单总结 二.什么是多态? 多态(Polymorphism)按字面的意思就是“多种状态”.在面 ...

  2. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类

    泛型:jdk1.5后的新特性,用于解决安全问题,是一个安全机制. 好处: 1.将运行时的异常出现问题classcastException.转移到了编译时期.方便程序员调试解决问题,让运行事情问题减少, ...

  3. (java基础)抽象类加泛型的理解

    今天在群里问了个基础问题,挨喷了..这更加激起了我对知识的渴望.也在此铭记一下,将来有经验了要对刚入门的童鞋们严格点,简单的东西要自己看...唉,程序员何苦为难程序猿呢.. 接下来简单总结下这个万能的 ...

  4. Java基础之十五 泛型

    第十五章 泛型 一般的类和方法,只能使用具体的类型:要么是基本类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制对代码的束缚就会很大. 在面对对象编程语言中,多态算是一种泛化机 ...

  5. JAVA基础_反射获取泛型参数类型

    我经常会想获取参数的实际类型,在Hibernate中就利用的这一点. domain: Person.java public class Person { // 编号 private Long id; ...

  6. Java基础(七)泛型数组列表ArrayList与枚举类Enum

    一.泛型数组列表ArrayList 1.在Java中,ArrayList类可以解决运行时动态更改数组的问题.ArrayList使用起来有点像数组,但是在添加或删除元素时,具有自动调节数组容量的功能,而 ...

  7. Java基础之浅谈泛型

    简单的介绍了Java泛型类型的使用.上手容易,深入很难.

  8. 【java基础学习】-【泛型】

    参考以下几位同学的总结来学习: http://www.cnblogs.com/lwbqqyumidi/p/3837629.html#!comments http://www.weixueyuan.ne ...

  9. Java基础之集合与泛型

    话不多说,直接上图 1.Collection集合小结 注意: 凡是使用哈希表保存对象的容器,保存的对象在容器中都是唯一的,唯一性是通过被保存对象的hashCode和equals方法共同确定: 凡是使用 ...

随机推荐

  1. 修改linux系统时间的方法(date命令)

    修改linux系统时间的方法(date命令) 来源:互联网 作者:佚名 时间:11-18 23:22:27 [大 中 小] date命令不仅可以显示系统当前时间,还可以用它来修改系统时间,下面简单的介 ...

  2. PHP 用QueryList抓取网页内容

    http://www.cnblogs.com/wb145230/p/4716403.html 之前抓取网页数据都是用Java Jsoup,前几天听说用PHP抓更方便,今天就简单研究了一下,主要是用Qu ...

  3. Arrays类的十大用法

    还有很多地方需要细细斟酌 0. 声明数组 String[] aArray = new String[5]; String[] bArray = {"a","b" ...

  4. 4 bytes (32 bits) or 8 bytes (64 bits)

    Computer Systems A Programmer's Perspective Second Edition BusesRunning throughout the system is a c ...

  5. java获取类路径

    String file = MessageTask3.class.getResource("").getFile(); File: public static final Stri ...

  6. 删除下标为n的数组值

    Array.prototype.del=function(n) { //n表示第几项,从0开始算起.//prototype为对象原型,注意这里为对象增加自定义方法的方法. if(n<0) //如 ...

  7. 图算法(一)——基本图算法(BFS,DFS及其应用)(2)

    2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和 ...

  8. 老调重弹:对kvo的封装思路

    关于kvo,kvo能做什么? kvo作为cocoa框架的重要特性之一,在底层框架中被大量使用.在特定的场合使用该特性往往能够带来难以想象的好处,让整个方案变得相当简洁和优雅.比如大名鼎鼎的下拉刷新的s ...

  9. C# 中==与Equals方法比较

    先来段代码,如下: static void Main(string[] args) { string a = new string(new char[] { 'h', 'e', 'l', 'l', ' ...

  10. 20145211 《Java程序设计》第1周学习总结——小荷才露尖尖角

    教材学习内容总结 Java语言概述 Java是SUN1995年推出的一门高级编程语言,完全面向对象,安全可靠,具有跨平台性(用其编写的语言在任何系统上都能运行,只需安装一个JVM) Java三大平台包 ...