Javase学习07-泛型
Javase学习07-泛型
1.问题导引:
/**
* @author: TSCCG
* @date: 2021/5/19
*/
public class OO {
public static void main(String[] args) {
Object[] oo = new Object[3];
oo[0] = "123";
oo[1] = 123;
for (int i = 0;i < oo.length;i++) {
((String)oo[i]).toString();
}
}
}
当执行以上代码时,毫无疑问会出现报错:
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
Object可以存放任意类型,上面代码在Object数组中添加了一个String类型和一个int类型,再使用时都以String的方式使用,因此程序出错。
为了解决避免这类问题,泛型应运而生。
2.泛型定义
泛型:把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类型
参数化类型:把类型当作是参数一样传递
<数据类型> 只能是引用类型,如 T
泛型类就是把泛型定义在类上,用户使用该类的时候,才把类型明确下来。
如此,用户明确了什么类型,该类就代表着什么类型。这样用户在使用的时候就不用担心强转的问题,运行时转换异常的问题了。
由此可派生出泛型接口、泛型方法等
3.泛型实例
3.1 首先定义一个泛型接口
该接口定义了增删查改等基本功能
package polymorphic;
/**
* @Author TSCCG
* @Date 2021/5/26 15:02
*/
public interface Super<T> {
//1.增
void add(T data);
//2.删
void delete(int index);
//3.查
T get(int index);
//4.改
void update(int index,T newData);
//5.长度
int size();
//6.是否为空
boolean isEmpty();
//7.输出
void print();
}
3.2 定义一个User类
package polymorphic;
import java.util.Objects;
/**
* @author: TSCCG
* @date: 2021/5/18
*/
public class User {
private String userName;
public User(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(userName, user.userName);
}
}
3.3 数组类实现泛型接口
package polymorphic;
/**
* @Author TSCCG
* @Date 2021/5/26 15:11
*/
public class SuperArray<T> implements Super <T> {
private Object[] arr;
private int currentIndex = -1;
public SuperArray(int size) {
arr = new Object[size];
}
public SuperArray() {
this(10);
}
//1.add
@Override
public void add(T data) {
currentIndex++;
if (currentIndex >= arr.length) {
Object[] newArr = new Object[currentIndex * 2];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
arr = newArr;
}
arr[currentIndex] = data;
}
//2.delete
@Override
public void delete(int index) {
rangeCheckForAdd(index);
for (int i = index; i < currentIndex; i++) {
arr[i] = arr[i + 1];
}
currentIndex--;
}
//3.get
@Override
public T get(int index) {
rangeCheckForAdd(index);
return (T)arr[index];
}
//4.update
@Override
public void update(int index, T newData) {
arr[index] = newData;
}
//5.size
@Override
public int size() {
return currentIndex + 1;
}
//6.isEmpty
@Override
public boolean isEmpty() {
return size() == 0;
}
//7.print
@Override
public void print() {
System.out.println("-----------数组--------------");
for (int i = 0; i <= currentIndex; i++) {
System.out.println(arr[i]);
}
}
//8.下标越界抛出异常
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size()) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size();
}
}
3.4 链表类实现泛型接口
3.4.1 泛型节点类:
package polymorphic;
/**
* @Author TSCCG
* @Date 2021/5/26 16:33
*/
public class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
}
3.4.2 链表主类:
package polymorphic;
import java.util.Objects;
/**
* @Author TSCCG
* @Date 2021/5/26 16:26
*/
public class SuperLink<T> implements Super<T>{
Node<T> head;
private int size = 0;
//1.add
@Override
public void add(T data) {
Node<T> newNode = new Node<>(data);
if (size == 0) {
head = newNode;
} else {
getEnd(head).setNext(newNode);
}
size++;
}
//2.delete
@Override
public void delete(int index) {
if (size > 0) {
rangeCheckForAdd(index);
Node<T> tempNode = getNode(index);
if (size == 1) {
head = null;
} else {
if (tempNode == head) {
head = tempNode.getNext();
}
getNode(index - 1).setNext(tempNode.getNext());
}
size--;
}
}
//3.get
@Override
public T get(int index) {
rangeCheckForAdd(index);
return getNode(index).getData();
}
//4.update
@Override
public void update(int index, T newData) {
if (size > 0) {
rangeCheckForAdd(index);
Objects.requireNonNull(getNode(index)).setData(newData);
}
}
//5.size
@Override
public int size() {
return size;
}
//6.isEmpty
@Override
public boolean isEmpty() {
return size() == 0;
}
//7.print
@Override
public void print() {
Node<T> tempNode = head;
while (tempNode != null) {
System.out.println(tempNode.getData());
tempNode = tempNode.getNext();
}
}
//8.1按下标找到节点
private Node<T> getNode(int index) {
rangeCheckForAdd(index);
Node<T> tempNode = head;
for (int i = 0; i < index; i++) {
tempNode = tempNode.getNext();
}
return tempNode;
}
//8.2找到尾节点
private Node<T> getEnd(Node<T> tempNode) {
if (tempNode.getNext() == null) {
return tempNode;
}
return getEnd(tempNode.getNext());
}
//8.下标越界抛出异常
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size()) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size();
}
}
3.5 测试类
package polymorphic;
/**
* @Author TSCCG
* @Date 2021/5/26 15:34
*/
public class Test {
public static void main(String[] args) {
// Super<Object> su = new SuperArray<>(3);
Super<Object> su = new SuperLink<>();
System.out.println("数组是否为空?" + su.isEmpty());
su.add(new User("张三"));
su.add(new User("李四"));
su.add("王五");
su.add("赵六");
System.out.println("----------添加后----------");
su.print();
su.delete(1);
System.out.println("----------删除后----------");
su.print();
Object temp = su.get(2);
System.out.println("查询:" + temp);
su.update(2,"阿珍");
System.out.println("----------更改后----------");
su.print();
System.out.println("数组是否为空?" + su.isEmpty());
System.out.println("此时元素个数为:" + su.size());
}
}
结果:
数组是否为空?true
----------添加后----------
User{userName='张三'}
User{userName='李四'}
王五
赵六
----------删除后----------
User{userName='张三'}
王五
赵六
查询:赵六
----------更改后----------
User{userName='张三'}
王五
阿珍
数组是否为空?false
此时元素个数为:3