赞
踩
本次泛型的使用是在之前超级数组和链表的基础上写的,这是之前数组和链表的链接
https://blog.csdn.net/wenqi1/article/details/116505175
个人根据学习的例子所理解的泛型为:相当于把多态中用到的方法抽离出来,单独放在一个类中进行调用
老师说以后用到的少,所以只要了解一下就行吧
1、接口类Super
- /**
- *
- * @param <T>
- * 表示泛型,以后要往这个类里面传User,就都会传到T里面
- */
- public interface Super <T>{
-
- /**
- * 添加数据
- * @param data
- */
- void add(T data);
-
- // 删除数据
- void delete(int index);
-
- // 获取新的头
- T get(int index);
-
- // 修改数据
- void updata(int index,T newData);
-
-
- // 输出,遍历
- void print();
-
- // 返回个长度
- int size();
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
2、Node类
- // 加上泛型
- public class Node<T> {
-
- // 具体存入的数据 指向下一个引用
- private T data;
- private Node next;
-
- public Node() { }
-
- public Node(T data, Node next) {
- this.data = data;
- this.next = next;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
-
- public Node getNext() {
- return next;
- }
-
- public void setNext(Node next) {
- this.next = next;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
3、SuperArray类
- // 包装超级数据,泛型
- public class SuperArray<T> implements Super<T>{
-
- // 使用泛型时,只能用Object,它是顶级父类
- private Object[] arr;
- private int currentIndex = -1;
-
- public SuperArray(int size){
- // 初始化,不初始化不能用
- arr = new Object[size];
- }
-
- public SuperArray(){
- this(10); // 默认数组长度为10
- }
-
- // 更新数据
- @Override
- public void updata(int index,T newData){
- arr[index] = newData;
- }
-
- // 给数组增加一个元素
- @Override
- public void add(T data){
- currentIndex++; // currentIndex加完就需要扩容
- // 如果currentIndex的长度不超出数组的长度,就和原来的数据相等
- // currentIndex的长度超出数组的长度,就给数组扩容
- if (currentIndex >= arr.length){
- Object[] temp = new Object[arr.length*2]; // 扩容两倍
- for(int i = 0; i <arr.length; i++){
- temp[i] = arr[i];
- }
- arr = temp;
- }
- arr[currentIndex] = data;
-
- }
-
- // 给数组删除一个元素
- @Override
- public void delete(int index){
- if (index < 0 || index > currentIndex){
- System.out.println("您输入的下标不合法");
- return;
- }
- for(int i = index; i < currentIndex; i++){
- arr[i] = arr[i+1];
- }
- currentIndex--;
- }
-
- //输出
- @Override
- public void print(){
- System.out.println("----结果----");
- for (int i = 0; i <= currentIndex; i++) {
- System.out.println(arr[i]+" ");
- }
- }
-
- // 链表所调用的构造函数,因为数组是Object的,所以要强转为T
- @Override
- public T get(int index){
- return (T)arr[index];
- }
-
- @Override
- public int size() {
- return currentIndex + 1;
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
4、SuperLink类
- public class SuperLink<T> implements Super<T>{
-
- // 设置链表的头
- private Node head;
- // 长度
- private int size;
-
- /**
- * 添加数据
- * @param data
- */
- @Override
- public void add(T data){
- // 1、让这个node变成头
- Node newHead = new Node(data,null);
- // 2、让新的头指向旧的头
- newHead.setNext(head);
- // 3、让新的头变成头
- head = newHead;
- // 长度++
- size++;
- }
-
- // 删除数据
- @Override
- public void delete(int index){
- if (index == 0){
- Node node = getNode(index);
- head = node.getNext();
- }else{
- Node node = getNode(index-1);
- node.setNext(node.getNext().getNext());
- }
- size--;
-
- }
-
- // 获取新的头
- @Override
- public T get(int index){
- // 因为getData提供者是Object,所以此处要强转,因为是要强转后面整个,所以要用括号括起来
- return (T)(getNode(index).getData());
- }
-
- // 修改数据
- @Override
- public void updata(int index,T newData){
- Node node = getNode(index);
- node.setData(newData);
- }
-
- // 有两个地方使用到了这个循环,就抽离出来写个方法
- // 获取新的头,getNext是往下走的意思
- private Node getNode(int index){
- Node node = head;
- for (int i = 0; i < index; i++) {
- node = node.getNext();
- }
- return node;
- }
-
- // 输出,遍历
- @Override
- public void print(){
- Node node = head;
- // 如果node不等于空就打印,等于空就不打印
- while (node != null){
- System.out.println(node.getData());
- // node获取下一个数据
- node = node.getNext();
- }
- }
-
- // 长度
- @Override
- public int size() {
- return size;
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
5、User类,这样数组和链表可以添加String的数据进去了
- public class User {
-
- private String username;
-
- private String password;
-
- public User(String username, String password) {
- this.username = username;
- this.password = password;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public void say(){
- System.out.println("我是:" + username);
- }
-
- @Override
- public String toString() {
- return "User{" +
- "username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
6、user的测试类
- public class TestUser {
-
- public static void main(String[] args) {
- // 要new泛型,必须在对象后边加上<>,<>里边写上你要泛型的数据
- Super<User> s = new SuperLink<>();
- s.add(new User("张三","123"));
- s.add(new User("李四","123"));
-
- for (int i = 0; i < s.size(); i++) {
- System.out.println(s.get(i));
- s.get(i).say();
- }
-
- // s.print();
-
-
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。