JavaSEDemo21注解与数据结构 青旅半醒 2021-09-25 07:30 255阅读 0赞 ### 文章目录 ### * 简介 * BeanUtils * 注解 * * 注解的作用 * 语法 * 注解可以用在哪些地方 * 声明注解的目标 * * 语法 * 注解的生命周期 * 注解属性 * 注解属性的默认值 * 如何使用注解 * 自定义注解步骤 * 注解数组 * 数据结构 * 基本的三种数据结构类型 * 线性表 * * 数组(典型的线性表) * 链表 * 链表和数组的区别 * 链表节点的添加 删除 插入 与 查看链表 # 简介 # * 本文是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; } }
相关 JavaSEDemo21注解与数据结构 文章目录 简介 BeanUtils 注解 注解的作用 语法 注解可以用在哪些地方 声明注解的目标 青旅半醒/ 2021年09月25日 07:30/ 0 赞/ 256 阅读
相关 JavaSEDemo20反射与内省 文章目录 简介 Method类的方法 String getName()得到方法名字 Class<?> getReturnType() 快来打我*/ 2021年09月25日 07:24/ 0 赞/ 373 阅读
相关 JavaSEDemo09 文章目录 简介 练习题目:统计字符串中不同字符的个数 代码示例 程序运行结果 final关键字 final关键字修 谁践踏了优雅/ 2021年09月25日 06:22/ 0 赞/ 335 阅读
相关 JavaSEDemo08 文章目录 简介 Object类的几个方法的使用或重写 equals hashCode finalize 抽象类 野性酷女/ 2021年09月25日 06:22/ 0 赞/ 317 阅读
相关 JavaSEDemo07 文章目录 简介 练习题:(多态)写一个函数,接收一个图形作为参数,打印出该图形的周长和面积 题目分析 代码示例 刺骨的言语ヽ痛彻心扉/ 2021年09月25日 06:18/ 0 赞/ 292 阅读
相关 JavaSEDemo06 文章目录 简介 显示继承与隐式继承 继承的重点 继承与构造方法 继承的优点和缺点 final关键字 多重继承时构造方法的执行情况 清疚/ 2021年09月25日 06:02/ 0 赞/ 329 阅读
相关 JavaSEDemo05 文章目录 简介 void方法中return语句的使用规范 可变API与不可变API 数据交换与方法传参 为什么不能用返回值类型来判断方法是否 比眉伴天荒/ 2021年09月25日 05:58/ 0 赞/ 362 阅读
还没有评论,来说两句吧...