泛型
一、 什么是泛型
- 定义
- Java泛型(generics)是从JDK 5开始使用。
- 泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。
- 问题导入
- 假定我们有这样一个需求:写一个排序方法,能够对整型数组、字符串数组甚至其他任何类型的数组进行排序,该如何实现?
- Java链表,队列,栈,怎样对多种数据类型操作。
- 怎么定义泛型
-
用<泛型标识符>
-
泛型标识符:
E - Element (在集合中使用,因为集合中存放的是元素)
T - Type(Java 类)
K - Key(键)
V - Value(值)
N - Number(数值类型)
? - 表示不确定的 java 类型 -
泛型里面所用的类型,全都是基本数据类型的包装类。(String不是基本数据类型,是一个类)。
-
包装类
| 类型 | 包装类 |
|---|---|
| int | Integer |
| byte | Byte |
| double | Double |
| char | Character |
- 其实用任何字母都可以表示,但以上更能表明达意,程序员们的约定熟成。
- 具体例子
- 泛型方法
- printArray()
package cbb;
public class Main {
//泛型方法 printArray
public static <E> void printArray(E[] Array) {
//增强型for循环
// for(E elem : Array) {
// System.out.print(elem+" ");
// }
// System.out.println();
for(int i=0; i<Array.length; i++) {
System.out.print(Array[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
//整型数组
Integer[] intArray = {1, 2, 3, 4, 5};
printArray(intArray);
//浮点型数组
Double[] doubleArray = {1.0, 1.2, 2.2, 3.3, 4.4};
printArray(doubleArray);
//字符型
Character[] charArray = {'a', 'b', 'c', 'd', 'e'};
printArray(charArray);
}
}
效果图

- Sort(),方法可以直接提出来用。
package cbb;
public class Generics {
/**
* 泛型排序
* @author 己千之
* @param <T>
* @param Array
*/
public <T extends Comparable<T>> void sort(T[] Array) {
// 验空
if(Array == null || Array.length == 0) {
System.out.println(-1);
}
// 冒泡排序
for(int i=0; i<Array.length; i++) {
for(int j=0; j<Array.length; j++) {
if(Array[i].compareTo(Array[j]) < 0) {
T temp = Array[i];
Array[i] = Array[j];
Array[j] = temp;
} // of if
} // of for j
} // of for i
// 输出
System.out.print("排序后:" );
for(int i=0; i<Array.length; i++) {
System.out.print(Array[i] + " ");
}
System.out.println();
} // of for Sort
}
测试类:
package cbb;
public class Test_Generics {
public static void main(String[] args) {
Generics G = new Generics();
// test1
Integer[] intArray = {5, 3, 6, 2, 8};
G.sort(intArray);
// test2
Character[] charArray = {'c', 'g', 'y', 't', 'a'};
G.sort(charArray);
// test3
Double[] doubleArray = {2.0, 1.1, 2.3, 6.9, 3.14, 6.18};
G.sort(doubleArray);
// test4
// 按照第一个字母来排序的,“我喜欢你像字母表顺序一样,从前往后”
String[] stringArray = {"I", "Love", "You", "A"};
G.sort(stringArray);
}
}
效果图:

- 泛型类
- 简单例子,理解泛型类的过程
package cbb;
class Generics<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return this.t;
}
}
public class Test_Generics {
public static void main(String[] args) {
Generics<Integer> intG = new Generics<Integer>();
Generics<String> stringG = new Generics<String>();
// test1 等价于 intG.set(new Integer(10));
intG.set(10);
System.out.println("整型值为 :" + intG.get());
// test2 等价于 stringG.set(new String("泛型"));
stringG.set("泛型");
System.out.println("字符串为 :" + stringG.get());
}
}
效果图:

- 改写JAVA第14天的栈,改为泛型
- SqStack类
//package pta;
/***这个是一个栈的类,入栈,出栈
* @author 己千之
* @version 1.0
* @param <T>
*/
public class SqStack<T> {
/**
* MAXSIZE = 10
*/
public static final int MAXSIZE = 100;
/**
* 这是栈的实际占量,整型
*/
private int stacksize;
/**
* 栈存放数据的数组
*/
private Object[] data;
/**
* 构造一个空栈
*/
SqStack() {
this.stacksize = 0;
this.data = new Object[MAXSIZE];
} // of SqStack
/**
* toString()
*/
public String toString() {
String result = "";
for(int i=0; i<this.stacksize; i++) {
result += data[i] + " ";
}
return result;
} // of toString
/**
* Push
* @author 己千之
* @param elem 类型T
* @return 成功或失败
*/
public boolean Push(T elem) {
//Stack full
if(this.stacksize==MAXSIZE) {
System.out.println("Stack full.");
return false;
}
this.data[this.stacksize] = elem;
this.stacksize++;
return true;
}//of Push
/**
* Pop
* @author 己千之
* @return T类型
*/
public T Pop() {
if(this.stacksize==0) {
System.out.println("StackEmpty.");
}//of StackEmpty
/**
* @SuppressWarnings("unchecked")
* 告诉编译器忽略 unchecked 警告信息
* 如使用List,ArrayList等未进行参数化产生的警告信息。
*/
@SuppressWarnings("unchecked")
T elem = (T) data[this.stacksize-1];
this.stacksize--;
return elem;
}//of Pop
/**
* 判断栈是否为空
* @author 己千之
* @return true or false 空或者不空
*/
public boolean StackEmpty() {
if(this.stacksize==0) {
//System.out.println("StackEmpty.");
return true;
}//Empty
else {
//System.out.println("Stack is not null.");
return false;
}
}//of StackEmpty
}
- 测试类
//package pta;
public class Test_SqStack {
public static void main(String[] args) {
/**
* 对整型,SqStack
*/
SqStack<Integer> intStack = new SqStack<Integer>();
// test1
System.out.println("栈是否为空:" + intStack.StackEmpty());
// test2
intStack.Push(1);
intStack.Push(2);
intStack.Push(4);
intStack.Push(6);
System.out.println("栈是否为空:" + intStack.StackEmpty());
System.out.println("栈元素:" + intStack.toString());
// test3
intStack.Pop();
System.out.println("栈元素:" + intStack.toString());
System.out.println("******************************************************");
/**
* 字符型,SqStack
*/
SqStack<Character> charStack = new SqStack<Character>();
// test1
System.out.println("栈是否为空:" + charStack.StackEmpty());
// test2
charStack.Push('a');
charStack.Push('b');
charStack.Push('d');
charStack.Push('f');
System.out.println("栈是否为空:" + charStack.StackEmpty());
System.out.println("栈元素:" + charStack.toString());
// test3
charStack.Pop();
System.out.println("栈元素:" + charStack.toString());
}
}
- 效果图

我建议先掌握这种,数据结构的栈的泛型类实现方法。有一种LinkList类的方法,我在问题集里面教你们。
- 改写JAVA第17天的队列,改为泛型
//package cb;
/**
* this is a LinkQueue
* FIFO
* @author 己千之
* @param T 泛型
* @version 1.1
*/
public class LinkQueue<T> {
/**
* 链表的结点,成员变量data,next
* @author 己千之
*
*/
class QNode {
T data;
QNode next;
/**
* 构造函数,赋初值
* @param QElemType
*/
public QNode(T QElemType) {
data = QElemType;
next = null;
}// of the constructor for QNode
public QNode() {
// TODO 自动生成的构造函数存根
}
}// of class QNode
/**
* 头指针和尾指针
*/
QNode head;
QNode tail;
/**
* 构造一个空队列
*/
public LinkQueue() {
head = new QNode();
tail = head;
}//of the constructor for LinkQueue
/**
* 队是否为空
* @author 己千之
* @return true or false
*/
public boolean QueueEmpty() {
//if(head == tail) return true;
if(head.next == null) return true;
else return false;
}
/**
* 入队 EnQueue
* @author 己千之
* @param elem 整型
* @return true
*/
public boolean EnQueue(T elem) {
QNode tempNode = new QNode(elem);
tail.next = tempNode;
tail = tempNode;
return true;
}
/**
* 出队
* @author 己千之
* @return tempValue 队头元素
*/
public T DeQueue() {
if(head == tail) {
System.out.println("Queue is Empty");
return null;
} //of if for EmptyQueue
T tempValue;
tempValue = head.next.data;
head.next = head.next.next;
if(head.next == null) {
tail = head;
}
return tempValue;
}
/**
* 重写toString
* @author 己千之
* @return tempString head.next.data...
*/
public String toString() {
String tempString = "";
if(head.next == null) {
return "Queue is Empty";
}
QNode tempNode = head.next;
while(tempNode!=null) {
tempString += tempNode.data + " ";
tempNode = tempNode.next;
}
return tempString;
}
}
- 测试类
package cb;
public class Test_LinkQueue {
public static void main(String[] args) {
// test1 Integer
LinkQueue<Integer> intQ = new LinkQueue<Integer>();
System.out.println(intQ.QueueEmpty());
intQ.EnQueue(1);
intQ.EnQueue(2);
intQ.EnQueue(3);
System.out.println(intQ.QueueEmpty());
System.out.println(intQ.toString());
// test2 Character
LinkQueue<Character> charQ = new LinkQueue<Character>();
System.out.println(charQ.QueueEmpty());
charQ.EnQueue('a');
charQ.EnQueue('b');
charQ.EnQueue('c');
System.out.println(charQ.QueueEmpty());
System.out.println(charQ.toString());
}
}
- 效果图

4844

被折叠的 条评论
为什么被折叠?



