在台科大的第二次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 Box<String> name1 = new Box<String>("corn"); Box<Integer> name2 = new Box<Integer>(12); //2 System.out.println("name:" + name1.getData()); System.out.println("name:" + name2.getData()); } } //3 class Box<T>{ class Box<T extends Number> { 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){ // 實例化泛型類 Point<Integer, Integer> p1 = new Point<Integer, Integer>(); p1.setX(10); p1.setY(20); int x = p1.getX(); int y = p1.getY(); System.out.println("This point is:" + x + ", " + y); Point<Double, String> p2 = new Point<Double, String>(); 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, T2>{ 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, 20
This point is:25.4, 東京180度
老師布置的是編寫一個插入算法的泛型類,對於數據結構非常差的我來說,簡直了,不過只有慢慢查資料慢慢改。下面放作業代碼。
import java.util.Comparator; public class LinkedList<T> { public <T extends Comparable> void sort(T x) { Node<T> HEAD=head.next,PRE=head.next; Node<T> newNode = new Node(x, null); Node<T> transNode= new Node(null,null); for(int i=0;i<count;i++){ if(i==0){ if(x.compareTo(head.data)< 0)//if x<head.data/x字典順序小於第一個數據 { newNode = new Node(x, head); transNode.next=newNode; head= transNode.next; count++; break; } HEAD=head.next; continue; } if(i==1){ if(x.compareTo(HEAD.data)< 0) { newNode = new Node(x,HEAD); head.next=newNode; count++; break; } else { HEAD=HEAD.next; PRE=head.next; continue; } } if(i<count-1){ if(x.compareTo(HEAD.data)< 0) { newNode = new Node(x,HEAD); PRE.next=newNode; count++; break; } else{ PRE=HEAD; HEAD=HEAD.next; } } if(i==count-1){ if(x.compareTo(HEAD.data)< 0) { newNode.next=HEAD; PRE.next=newNode; newNode.data=x; count++; break; } else HEAD.next=newNode; newNode.data=x; newNode.next=null; count++; } } } public static class Node<T> { T data; Node next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public String toString() { return data.toString(); } } static int count; Node<T> 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<T> newNode = new Node(entry, head); head = newNode; count++; return true; } Node<T> node = head; for (int i = 0; i < index - 1; i++, node = node.next); //System.out.println("prev "+node); Node<T> 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<T> 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<String> 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<Integer> 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<Double> 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:ccc apple, banana, ccc, melon, pear, zzz count:6 ------------------ 1, 3, 5, 7, 9 *********** insertion sort:4 1, 3, 4, 5, 7, 9 count:6 ------------------ 1.1, 3.3, 5.1, 7.1, 9.1 *********** insertion sort:4.1 1.1, 3.3, 4.1, 5.1, 7.1, 9.1 count:6 ------------------
