简介
 
- 本文是2021/04/20整理的笔记
- 赘述可能有点多,还请各位朋友耐心阅读
- 本人的内容和答案不一定是最好最正确的,欢迎各位朋友评论区指正改进
练习题
 
练习1
 
- 题目:
 实现 一个方法:
 public static Object execute(String className, String methodName, Object args[]);
 实现“通过类的名字、方法名字、方法参数调调用方法,返回值为该方法的返回值。” 的功能。用反射调用.
- 答案:
- User类
package day0419.demo01$2;
public class User {
    public void eat(String name,Integer count){
        System.out.println("我吃了 " +count+"个"+name );
    }
}
 
- Test类
package day0419.demo01$2;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
    public static void main(String[] args) throws 
    ClassNotFoundException,
     NoSuchMethodException, 
     InvocationTargetException, 
     InstantiationException, 
     IllegalAccessException {
        execute("day0419.demo01$2.User", "eat", new Object[]{ "炸鸡腿",5});
    }
    public static Object execute
    (String className, String methodName, Object args[]) throws 
    ClassNotFoundException, 
    IllegalAccessException, 
    InstantiationException,
     NoSuchMethodException, 
     InvocationTargetException {
        
        Class<?> aClass = Class.forName(className);
        
        Object o = aClass.newInstance();
        
        Class[] paramTypes = new Class[args.length];
        
        for (int i = 0; i < paramTypes.length; i++) {
            paramTypes[i] = args[i].getClass();
        }
        
        Method method = aClass.getMethod(methodName, paramTypes);
        
        Object invoke = method.invoke(o, args);
        return invoke;
    }
}
 
- 程序运行结果
  
练习2
 
- 题目
 自定义注解WebInitParam以及WebServlet,其中WebInitParam定义字符串类型属性name及value;WebServlet解定义字符串类型属性name以及displayName;int类型属性loadOnStartup ;boolean类型属性asyncSupported;String[]类型 属性urlPatterns;WebInitParam []类型属性initParams。在类LoginServlet中使用注解。
 答案
 
栈
 
栈的特点
 
 
创建栈
 
public class MyStack{
private int[] stack;
private int top;
private final int bottom;
private final int SIZE;
public MyStack(int size){
SIZE = size;
bottom = 0;
top = bottom;
stack = new int[SIZE];
}
}
 
判断栈是否为空
 
public void isEmpty(){
return bottom == top;
}
 
判断栈是否满
 
public void isFull(){
return top == SIZE;
}
 
压栈(入栈)
 
public void push(int data){
if(isFull()){
throw new IllegalStateException("队列已满");
}else{
stack[top++] = data;
}
}
 
弹栈(出栈)
 
public int pop(){
if(isFull()){
throw new IllegalStateException("队列为空");
}else{
return stack[--top];
}
}
 
环形队列
 
环形队列的特点
 
 
创建队列
 
public class MyQueue {
    
    private int[] queue;
    private int head;
    private int end;
    private final int LENGTH;
    
    public MyQueue(int length) {
        LENGTH = length;
        head = 0;
        end = 0;
        queue = new int[LENGTH];
    }
    }
 
得到头指针和尾指针的下一个位置
 
public int next(int index){
return (index + 1) % LENGTH;
}
 
判断队列是否为空
 
public boolean isEmpty(){
        return head == end;
    }
 
判断队列是否未满
 
public boolean isFull(){
        return next(end) == head;
    }
 
入队
 
 public void insert(int data){
        if(isFull()){
            throw new IllegalStateException("队列已满,无法入队");
        }else {
            queue[end] = data;
            end = next(end);
        }
    }
 
出队
 
 public int get(){
        if(isEmpty()){
            throw new IllegalStateException("队列为空,无法出队");
        }else {
            head = next(head);
            return queue[head];
        }
    }
 
树
 
创建树(中序遍历的二叉树)
 
public class MyBinaryTree {
    
    private class Node{
      
        private int key;
        private int value;
        private Node left;
        private Node right;
        
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
        
        @Override
        public String toString() {
            return "Node{" +
                    "key=" + key +
                    ", value=" + value +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }
    
    private Node root;
    
    public MyBinaryTree() {
        root = null;
    }
    }
 
二叉树插入的方法
 
 
    public void addNode(int key,int value){
        Node newNode = new Node(key,value);
        
        if(root == null || root.key == key){
          root = new Node(key,value);
          return;
        }
        Node current = root;
        while(true){
            
            if(key < current.key){
                if(current.left == null) {
                    current.left = newNode;
                    break;
                }else {
                   current = current.left;
                }
                
            }else {
                if(current.right == null){
                    current.right = newNode;
                    break;
                }else {
                    current = current.right;
                }
            }
        }
    }
 
二叉树查找结点的方法
 
    public Node getNode(int key){
        
        if(root ==null){
            return null;
        }
        
        if(root.key == key){
        return  root;
        }
        
        Node current = root;
        while(current.key != key){
             if(key < current.key){
                 current = current.left;
                 if(current ==null){
                     return null;
                 }
             }else {
                 current = current.right;
                 if(current == null){
                     return null;
                 }
             }
        }
        return current;
    }
 
二叉树根据key得到value的方法
 
    public int getValue(int key){
        if(getNode(key) != null) {
            System.out.println("查找成功");
            return getNode(key).value;
        }else {
            System.out.println("查找失败");
            return -404;
        }
    }