文章目录
简介
- 本文是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)表示该注解能用于其它注解
注解的生命周期
语法:
- 源代码阶段
@Retention(RetentionPolicy.SOURCE) - Class类对象阶段
@Retention(RetentionPolicy.CLASS) - Runtime运行时阶段
@Retention(RetentionPolicy.RUNTIME)
注解属性
注解对象.属性名( )
注解属性的默认值
语法 default 值
代码示例
public @interface MyAnnotation{
public String name() default "张三";
}
如何使用注解
为注解属性设置值:
自定义注解步骤
- 自定义注解类
- 再写一个类,加注解
- 写测试类
注解数组
- 定义多个自定义注解
- 在类上应用多个注解
- 在测试类得到注解数组
数据结构
基本的三种数据结构类型
线性表
树
图
线性表
- 线性表,一个有序的序列结构,除头节点和尾节点外,每个节点有且只有一个前序节点和一个后序节点。
数组(典型的线性表)
- 特点
- 通过下标索引对数组元素进行访问
- 数组中每个元素的数据类型必须一致
- 数组的大小一旦确定就不能变更
- 缺点
- 插入删除的效率非常低
- 数组大小不可变,无法实现动态生成
链表
链表和数组的区别
链表节点的添加 删除 插入 与 查看链表
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;
}
}