在台科大的第二次JAVA作业,老师课上讲的内容是泛型。
泛型(generic),泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。通俗点说就是一个盒子本来只能放int,改成泛型之后就可以灵活地根据你的要求放你的类型,如int,double,char,string等。下面看两个例子:
第一个例子中声明了一个Box类型,并继承了Number,也就是说Box只能用于对数的声明,如int,double,而如果改成3处的代码,则1,2处的代码可以正常运行,因为没有再继承Number,Box泛型也就包括了char,string等。
public class TestDemo2 { public static void main(String[] args) {//1 Boxname1 = new Box ("corn"); Box name2 = new Box (12);//2 System.out.println("name:" + name1.getData()); System.out.println("name:" + name2.getData()); }}//3 class Box { class Box { private T data; public Box() { } public Box(T data) { this.data = data; } public T getData() { return data; }}
运行结果:
name:12
第二个例子声明了一个Point类型,里面包含两个坐标的设置和获取。
public static void main(String[] args){ // 实例化泛型类 Pointp1 = new Point (); p1.setX(10); p1.setY(20); int x = p1.getX(); int y = p1.getY(); System.out.println("This point is:" + x + ", " + y); Point p2 = new Point (); p2.setX(25.4); p2.setY("东京180度"); double m = p2.getX(); String n = p2.getY(); System.out.println("This point is:" + m + ", " + n); }}// 定义泛型类class Point { T1 x; T2 y; public T1 getX() { return x; } public void setX(T1 x) { this.x = x; } public T2 getY() { return y; } public void setY(T2 y) { this.y = y; }}
运行结果:
This point is:10, 20This point is:25.4, 东京180度
老师布置的是编写一个插入算法的泛型类,对于数据结构非常差的我来说,简直了,不过只有慢慢查资料慢慢改。下面放作业代码。
import java.util.Comparator;public class LinkedList{ public void sort(T x) { Node HEAD=head.next,PRE=head.next; Node newNode = new Node(x, null); Node transNode= new Node(null,null); for(int i=0;i { T data; Node next; public Node(T data, Node next) { this.data = data; this.next = next; } public String toString() { return data.toString(); } } static int count; Node head; public LinkedList() { count = 0; head = null; } public boolean insert(int index, T entry) { if (index < 0 || index > count) { return false; } if (index == 0) { Node newNode = new Node(entry, head); head = newNode; count++; return true; } Node node = head; for (int i = 0; i < index - 1; i++, node = node.next); //System.out.println("prev "+node); Node newNode = new Node(entry, node.next); node.next = newNode; count++; return true; } public int size() { return count; } public boolean isEmpty() { return count == 0; } public String toString() { StringBuilder s = new StringBuilder(); for (Node node = head; node != null; node = node.next) { if (node != head) { s.append(", "); } s.append(node.toString()); } return s.toString(); } public class Member { String firstname; String lastname; int age; public Member(String firstname, String lastname, int age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } public String toString() { return firstname + " " + lastname + " age=" + age; } } public static void main(String args[]) { LinkedList fruits = new LinkedList(); fruits.insert(0, "apple"); fruits.insert(1, "banana"); fruits.insert(2, "melon"); fruits.insert(3, "pear"); fruits.insert(4, "zzz"); System.out.println(fruits); System.out.println("***********"); System.out.println("insertion sort:ccc"); fruits.sort("ccc"); System.out.println(fruits); System.out.print("count:"); System.out.println(fruits.count); System.out.println("------------------"); LinkedList IntNum = new LinkedList(); IntNum.insert(0, 1); IntNum.insert(1, 3); IntNum.insert(2, 5); IntNum.insert(3, 7); IntNum.insert(4, 9); System.out.println(IntNum); System.out.println("***********"); System.out.println("insertion sort:4"); IntNum.sort(4); System.out.println(IntNum); System.out.print("count:"); System.out.println(IntNum.count); System.out.println("------------------"); LinkedList DouNum = new LinkedList(); DouNum.insert(0, 1.1); DouNum.insert(1, 3.3); DouNum.insert(2, 5.1); DouNum.insert(3, 7.1); DouNum.insert(4, 9.1); System.out.println(DouNum); System.out.println("***********"); System.out.println("insertion sort:4.1"); DouNum.sort(4.1); System.out.println(DouNum); System.out.print("count:"); System.out.println(DouNum.count); System.out.println("------------------"); }}
运行结果:
apple, banana, melon, pear, zzz***********insertion sort:cccapple, banana, ccc, melon, pear, zzzcount:6------------------1, 3, 5, 7, 9***********insertion sort:41, 3, 4, 5, 7, 9count:6------------------1.1, 3.3, 5.1, 7.1, 9.1***********insertion sort:4.11.1, 3.3, 4.1, 5.1, 7.1, 9.1count:6------------------