JavaSEDemo21注解与数据结构

简介

  • 本文是2021/04/19整理的笔记
  • 赘述可能有点多,还请各位朋友耐心阅读
  • 本人的内容和答案不一定是最好最正确的,欢迎各位朋友评论区指正改进

BeanUtils

注解

Annotation:注解,是给计算机看的
注释:是给程序员看的

  • 从JDK1.5版本开始引入,与类、接口、枚举是同一个层次。

注解的作用

编写文档:生成doc文档
代码分析:使用反射对代码进行分析
编译检查:使用代码里的表示的元数据让编译器能够实现基本的编译检查

语法

public @interface 注解名字{
//注解属性
}

public @interface MyAnnotation{
}

注解可以用在哪些地方

  • 常用
    TYPE 类 接口 枚举
    PARAMETER 方法参数
    METHOD 方法
    FIELD 成员变量(使用最频繁)
  • 不常用
    PACKAGE 包
    LOCAL_VARIABLE 局部变量
    CONSTRUCTOR 构造器
    ANNOTATION_TYPE 表示该注解能用于注解类型

声明注解的目标

语法

@Target(value = ElementType.TYPE)类 接口 枚举
@Target(value = ElementType.PARAMETER)方法参数
@Target(value = ElementType.METHOD)方法
@Target(value = ElementType.FIELD)成员变量
@Target(value = ElementType.PACKAGE)包
@Target(value = ElementType.LOCAL_VARIABLE)局部变量
@Target(value = ElementType.CONSTRUCTOR)构造器
@Target(value = ElementType.ANNOTATION_TYPE)表示该注解能用于其它注解

注解的生命周期

语法:

  1. 源代码阶段
    @Retention(RetentionPolicy.SOURCE)
  2. Class类对象阶段
    @Retention(RetentionPolicy.CLASS)
  3. Runtime运行时阶段
    @Retention(RetentionPolicy.RUNTIME)

注解属性

注解对象.属性名( )

注解属性的默认值

语法 default 值
代码示例

public @interface MyAnnotation{
public String name() default "张三";
}

如何使用注解

为注解属性设置值:

自定义注解步骤

  1. 自定义注解类
  2. 再写一个类,加注解
  3. 写测试类

注解数组

  1. 定义多个自定义注解
  2. 在类上应用多个注解
  3. 在测试类得到注解数组

数据结构

基本的三种数据结构类型

线性表

线性表

  • 线性表,一个有序的序列结构,除头节点和尾节点外,每个节点有且只有一个前序节点和一个后序节点。

数组(典型的线性表)

  • 特点
  1. 通过下标索引对数组元素进行访问
  2. 数组中每个元素的数据类型必须一致
  3. 数组的大小一旦确定就不能变更
  • 缺点
  1. 插入删除的效率非常低
  2. 数组大小不可变,无法实现动态生成

链表

链表和数组的区别

链表节点的添加 删除 插入 与 查看链表

public class MyLine {
    //成员内部类Node节点
    public class Node {
        //数据的键
        private int key;
        //数据的值
        private int value;
        //指向下一个节点:下一个节点的地址
        private Node next;

        //无参构造
        public Node() {
        }

        //有参构造
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
            next = null;
        }
    }

    //头节点
    private Node head;

    public MyLine(Node head) {
        head = null;
    }

    public MyLine() {
    }

    /*
    向尾部添加节点的方法
     */
    public void addNode(int key, int value) {
        //判断头节点是否为空
        if (head == null) {
            //如果为空,则表示链表为空,创建一个新节点作为头节点
            head = new Node(key, value);
            //如果不为空
        } else {
            //将头节点的地址赋值交给当前节点
            Node current = head;
            //循环判断尾节点的位置
            while (current.next != null) {
                //只要下一个节点指向不为空,当前节点一直向后移
                current = current.next;
            }
            //此处current代表最后一个元素,创建一个新节点作为尾节点
            current.next = new Node(key, value);
        }
    }


    /*
    查看链表的方法
     */
    public void view() {
        Node current = head;
        while (current != null) {
            System.out.println(current.key + " " + current.value);
            current = current.next;
        }
    }

    /*
    删除链表元素的方法
     */
    public boolean delNode(int key) {
        Node current;
        Node parent;
        //如果头节点为空,则表示此链表为空,无法删除,返回false
        if (head == null) {
            return false;
        }
        //如果要删除的节点为头节点
        if (head.key == key) {
            head = head.next;
            return true;
            //如果要删除的节点不为头节点,当前节点一直向后移
        } else {
            current = head.next;
            parent = head;
            /* 优化1:
            如果当前key不是我们要的key,当前节点一直向后移
            while (current.key != key) {
                current = current.next;
                parent = parent.next;
            }
            //如果当前为空
            if (current == null) {
                return false;
            }
            //如果不为空
            parent.next = current.next;
            return true;*/

            /* 优化2
            判断当前节点是不是尾节点
            while (current.next != null ) {
                if (current.key == key) {
                    parent.next = current.next;
                    return true;
                }
                //一直后移
                current = current.next;
                parent = parent.next;
                //如果当前节点的key是我们要找的key
                }
            }
        return false;*/

            /*
            优化3:
             */
            while(current.key != key){
                current = current.next;
                parent = parent.next;
                if(current == null) {
                    return false;
                }
            }
            parent.next = current.next;
            return true;
        }
    }

    //链表两个节点中间插入元素
    public boolean insert1(Node newNode,int index){
        Node preNode = head;

        //如果头节点为空,或者插入索引为0
        if(preNode == null || index == 0){
            newNode.next = preNode;
            head = newNode;
            System.out.println("MyLine.insert([newNode, index])方法");
            return true;
        }else {
            //如果链表遍历完毕或者找到了要插入位置的前驱
            while(preNode.next !=null && index-- >0){
                preNode =preNode.next;
            }
            newNode.next = preNode.next;
            preNode.next = newNode;
            return true;
        }
    }

 public boolean insert2(Node newnode,int index){
        //当前节点
        Node current;
        if(index == 0){
            newnode.next = head;
            head = newnode;
            return true;
        }
        current = head;
     for (int i = 0; i < index - 1; i++) {
         if(current.next != null){
               current = current.next;
         }else {
             System.out.println("超出链表长度");
             return false;
         }
     }
     newnode.next = current.next;
     current.next = newnode;
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香鱼嫩虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值