From a80f04418c70802cf680dc8deb1d4f8627919404 Mon Sep 17 00:00:00 2001 From: zain Date: Mon, 13 Apr 2020 00:24:44 +0800 Subject: [PATCH] week 6 --- second/week_06/87/622-MyCircularQueue.java | 58 +++++++++++ second/week_06/87/641-MyCircularDeque.java | 96 ++++++++++++++++++ second/week_06/87/705-MyHashSet.java | 65 ++++++++++++ second/week_06/87/706-MyHashMap.java | 87 ++++++++++++++++ second/week_06/87/707-MyLinkedList.java | 104 +++++++++++++++++++ second/week_06/87/Class.md | 112 +++++++++++++++++++++ second/week_06/87/Spring.md | 53 ++++++++++ 7 files changed, 575 insertions(+) create mode 100644 second/week_06/87/622-MyCircularQueue.java create mode 100644 second/week_06/87/641-MyCircularDeque.java create mode 100644 second/week_06/87/705-MyHashSet.java create mode 100644 second/week_06/87/706-MyHashMap.java create mode 100644 second/week_06/87/707-MyLinkedList.java create mode 100644 second/week_06/87/Class.md create mode 100644 second/week_06/87/Spring.md diff --git a/second/week_06/87/622-MyCircularQueue.java b/second/week_06/87/622-MyCircularQueue.java new file mode 100644 index 0000000..bcba57f --- /dev/null +++ b/second/week_06/87/622-MyCircularQueue.java @@ -0,0 +1,58 @@ +class MyCircularQueue { + + private int[] queue; + private int headIndex; + private int count; + private int capacity; + + /** Initialize your data structure here. Set the size of the queue to be k. */ + public MyCircularQueue(int k) { + this.capacity = k; + this.queue = new int[k]; + this.headIndex = 0; + this.count = 0; + } + + /** Insert an element into the circular queue. Return true if the operation is successful. */ + public boolean enQueue(int value) { + if (this.count == this.capacity) + return false; + this.queue[(this.headIndex + this.count) % this.capacity] = value; + this.count += 1; + return true; + } + + /** Delete an element from the circular queue. Return true if the operation is successful. */ + public boolean deQueue() { + if (this.count == 0) + return false; + this.headIndex = (this.headIndex + 1) % this.capacity; + this.count -= 1; + return true; + } + + /** Get the front item from the queue. */ + public int Front() { + if (this.count == 0) + return -1; + return this.queue[this.headIndex]; + } + + /** Get the last item from the queue. */ + public int Rear() { + if (this.count == 0) + return -1; + int tailIndex = (this.headIndex + this.count - 1) % this.capacity; + return this.queue[tailIndex]; + } + + /** Checks whether the circular queue is empty or not. */ + public boolean isEmpty() { + return (this.count == 0); + } + + /** Checks whether the circular queue is full or not. */ + public boolean isFull() { + return (this.count == this.capacity); + } +} \ No newline at end of file diff --git a/second/week_06/87/641-MyCircularDeque.java b/second/week_06/87/641-MyCircularDeque.java new file mode 100644 index 0000000..4cee6fd --- /dev/null +++ b/second/week_06/87/641-MyCircularDeque.java @@ -0,0 +1,96 @@ +public class MyCircularDeque { + private int capacity; + private int[] arr; + private int front; + private int rear; + + /** + * Initialize your data structure here. Set the size of the deque to be k. + */ + public MyCircularDeque(int k) { + capacity = k + 1; + arr = new int[capacity]; + front = 0; + rear = 0; + } + + /** + * Adds an item at the front of Deque. Return true if the operation is successful. + */ + public boolean insertFront(int value) { + if (isFull()) { + return false; + } + front = (front - 1 + capacity) % capacity; + arr[front] = value; + return true; + } + + /** + * Adds an item at the rear of Deque. Return true if the operation is successful. + */ + public boolean insertLast(int value) { + if (isFull()) { + return false; + } + arr[rear] = value; + rear = (rear + 1) % capacity; + return true; + } + + /** + * Deletes an item from the front of Deque. Return true if the operation is successful. + */ + public boolean deleteFront() { + if (isEmpty()) { + return false; + } + front = (front + 1) % capacity; + return true; + } + + /** + * Deletes an item from the rear of Deque. Return true if the operation is successful. + */ + public boolean deleteLast() { + if (isEmpty()) { + return false; + } + rear = (rear - 1 + capacity) % capacity; + return true; + } + + /** + * Get the front item from the deque. + */ + public int getFront() { + if (isEmpty()) { + return -1; + } + return arr[front]; + } + + /** + * Get the last item from the deque. + */ + public int getRear() { + if (isEmpty()) { + return -1; + } + return arr[(rear - 1 + capacity) % capacity]; + } + + /** + * Checks whether the circular deque is empty or not. + */ + public boolean isEmpty() { + return front == rear; + } + + /** + * Checks whether the circular deque is full or not. + */ + public boolean isFull() { + return (rear + 1) % capacity == front; + } +} \ No newline at end of file diff --git a/second/week_06/87/705-MyHashSet.java b/second/week_06/87/705-MyHashSet.java new file mode 100644 index 0000000..4da8be6 --- /dev/null +++ b/second/week_06/87/705-MyHashSet.java @@ -0,0 +1,65 @@ +class MyHashSet { + private Bucket[] bucketArray; + private int keyRange; + + /** Initialize your data structure here. */ + public MyHashSet() { + this.keyRange = 769; + this.bucketArray = new Bucket[this.keyRange]; + for (int i = 0; i < this.keyRange; ++i) + this.bucketArray[i] = new Bucket(); + } + + protected int _hash(int key) { + return (key % this.keyRange); + } + + public void add(int key) { + int bucketIndex = this._hash(key); + this.bucketArray[bucketIndex].insert(key); + } + + public void remove(int key) { + int bucketIndex = this._hash(key); + this.bucketArray[bucketIndex].delete(key); + } + + /** Returns true if this set contains the specified element */ + public boolean contains(int key) { + int bucketIndex = this._hash(key); + return this.bucketArray[bucketIndex].exists(key); + } +} + + +class Bucket { + private LinkedList container; + + public Bucket() { + container = new LinkedList(); + } + + public void insert(Integer key) { + int index = this.container.indexOf(key); + if (index == -1) { + this.container.addFirst(key); + } + } + + public void delete(Integer key) { + this.container.remove(key); + } + + public boolean exists(Integer key) { + int index = this.container.indexOf(key); + return (index != -1); + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * MyHashSet obj = new MyHashSet(); + * obj.add(key); + * obj.remove(key); + * boolean param_3 = obj.contains(key); + */ \ No newline at end of file diff --git a/second/week_06/87/706-MyHashMap.java b/second/week_06/87/706-MyHashMap.java new file mode 100644 index 0000000..5cf580c --- /dev/null +++ b/second/week_06/87/706-MyHashMap.java @@ -0,0 +1,87 @@ +class Pair { + public U first; + public V second; + + public Pair(U first,V second) { + this.first = first; + this.second = second; + } +} + +class Bucket { + private List> bucket; + + public Bucket() { + this.bucket = new LinkedList>(); + } + + public Integer get(Integer key) { + for (Pair pair : this.bucket) { + if (pair.first.equals(key)) + return pair.second; + } + return -1; + } + + public void update(Integer key,Integer value) { + boolean found = false; + for (Pair pair : this.bucket) { + if (pair.first.equals(key)) { + pair.second = value; + found = true; + } + } + if (!found) { + this.bucket.add(new Pair(key,value)); + } + } + + public void remove(Integer key) { + for (Pair pair : this.bucket) { + if (pair.first.equals(key)) { + this.bucket.remove(pair); + break; + } + } + } +} + +class MyHashMap { + private int key_space; + private List hash_table; + + /** Initialize your data structure here. */ + public MyHashMap() { + this.key_space = 2069; + this.hash_table = new ArrayList(); + for (int i = 0; i < this.key_space; ++i) { + this.hash_table.add(new Bucket()); + } + } + + /** value will always be non-negative. */ + public void put(int key, int value) { + int hash_key = key % this.key_space; + this.hash_table.get(hash_key).update(key,value); + } + + /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ + public int get(int key) { + int hash_key = key % this.key_space; + return this.hash_table.get(hash_key).get(key); + } + + /** Removes the mapping of the specified value key if this map contains a mapping for the key */ + public void remove(int key) { + int hash_key = key % this.key_space; + this.hash_table.get(hash_key).remove(key); + } +} + +/** + * Your MyHashMap object will be instantiated and called as such: + * MyHashMap obj = new MyHashMap(); + * obj.put(key,value); + * int param_2 = obj.get(key); + * obj.remove(key); + */ \ No newline at end of file diff --git a/second/week_06/87/707-MyLinkedList.java b/second/week_06/87/707-MyLinkedList.java new file mode 100644 index 0000000..c2b2c2a --- /dev/null +++ b/second/week_06/87/707-MyLinkedList.java @@ -0,0 +1,104 @@ +public class ListNode { + int val; + ListNode next; + ListNode prev; + ListNode(int x) { val = x; } +} + +class MyLinkedList { + int size; + ListNode head, tail; + public MyLinkedList() { + size = 0; + head = new ListNode(0); + tail = new ListNode(0); + head.next = tail; + tail.prev = head; + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + public int get(int index) { + if (index < 0 || index >= size) return -1; + + ListNode curr = head; + if (index + 1 < size - index) + for(int i = 0; i < index + 1; ++i) curr = curr.next; + else { + curr = tail; + for(int i = 0; i < size - index; ++i) curr = curr.prev; + } + + return curr.val; + } + + /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ + public void addAtHead(int val) { + ListNode pred = head, succ = head.next; + + ++size; + ListNode toAdd = new ListNode(val); + toAdd.prev = pred; + toAdd.next = succ; + pred.next = toAdd; + succ.prev = toAdd; + } + + /** Append a node of value val to the last element of the linked list. */ + public void addAtTail(int val) { + ListNode succ = tail, pred = tail.prev; + + ++size; + ListNode toAdd = new ListNode(val); + toAdd.prev = pred; + toAdd.next = succ; + pred.next = toAdd; + succ.prev = toAdd; + } + + /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ + public void addAtIndex(int index, int val) { + if (index > size) return; + + if (index < 0) index = 0; + + ListNode pred, succ; + if (index < size - index) { + pred = head; + for(int i = 0; i < index; ++i) pred = pred.next; + succ = pred.next; + } + else { + succ = tail; + for (int i = 0; i < size - index; ++i) succ = succ.prev; + pred = succ.prev; + } + + ++size; + ListNode toAdd = new ListNode(val); + toAdd.prev = pred; + toAdd.next = succ; + pred.next = toAdd; + succ.prev = toAdd; + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + public void deleteAtIndex(int index) { + if (index < 0 || index >= size) return; + + ListNode pred, succ; + if (index < size - index) { + pred = head; + for(int i = 0; i < index; ++i) pred = pred.next; + succ = pred.next.next; + } + else { + succ = tail; + for (int i = 0; i < size - index - 1; ++i) succ = succ.prev; + pred = succ.prev.prev; + } + + --size; + pred.next = succ; + succ.prev = pred; + } +} \ No newline at end of file diff --git a/second/week_06/87/Class.md b/second/week_06/87/Class.md new file mode 100644 index 0000000..cc08a38 --- /dev/null +++ b/second/week_06/87/Class.md @@ -0,0 +1,112 @@ +# Class + +>每个类的运行时的**类型信息**就是用Class对象表示的。它包含了与类有关的信息。其实我们的实例对象就通过Class对象来创建的。Java使用Class对象执行其RTTI(运行时类型识别,Run-Time Type Identification),多态是基于RTTI实现的。 +> +>Class类被创建后的对象就是Class对象,注意,Class对象表示的是自己手动编写类的类型信息,比如创建一个Shapes类,那么,JVM就会创建一个Shapes对应Class类的Class对象,该Class对象保存了Shapes类相关的类型信息。 +> +>实际上在Java中每个类都有一个Class对象,每当我们编写并且编译一个新创建的类就会产生一个对应Class对象并且这个Class对象会被保存在同名.class文件里(编译后的字节码文件保存的就是Class对象),那为什么需要这样一个Class对象呢?是这样的,当我们new一个新对象或者引用静态成员变量时,Java虚拟机(JVM)中的类加载器子系统会将对应Class对象加载到JVM中,然后JVM再根据这个类型信息相关的Class对象创建我们需要实例对象或者提供静态变量的引用值。需要特别注意的是,手动编写的每个class类,无论创建多少个实例对象,在JVM中都只有一个Class对象,**即在内存中每个类有且只有一个相对应的Class对象**。 +> +>实际上所有的类都是在对其第一次使用时动态加载到JVM中的,当程序第一次调用类的静态成员时,就会加载这个被使用的类(实际上加载的就是这个类的字节码文件),注意,使用new操作符创建类的新实例对象也会被当作调用类的静态成员(构造函数也是类的静态方法),由此看来Java程序在它们开始运行之前并非被完全加载到内存的,其各个部分是按需加载,所以在使用该类时,类加载器首先会检查这个类的Class对象是否已被加载(类的实例对象创建时依据Class对象中类型信息完成的),如果还没有加载,默认的类加载器就会先根据类名查找.class文件(编译后Class对象被保存在同名的.class文件中),在这个类的字节码文件被加载时,它们必须接受相关验证,以确保其没有被破坏并且不包含不良Java代码(这是java的安全机制检测),完全没有问题后就会被动态加载到内存中,此时相当于Class对象也就被载入内存了(毕竟.class字节码文件保存的就是Class对象),同时Class对象也就可以被用来创建这个类的所有实例对象。 + + + +## forName + +```java +public static void main(String[] args) { + + try{ +   //通过Class.forName获取Gum类的Class对象 +   Class clazz=Class.forName("Gum"); +   System.out.println("forName=clazz:"+clazz.getName()); + }catch (ClassNotFoundException e){ +   e.printStackTrace(); + } + + //通过实例对象获取Gum的Class对象 + Gum gum = new Gum(); + Class clazz2 = gum.getClass(); + System.out.println("new=clazz2:"+clazz2.getName()); +} +``` + +## newInstance + +```java +@CallerSensitive +public T newInstance() + throws InstantiationException, IllegalAccessException +{ + if (System.getSecurityManager() != null) { + // 校验是否有权 + checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false); + } + + // NOTE: the following code may not be strictly correct under + // the current Java memory model. + + // Constructor lookup + if (cachedConstructor == null) { + if (this == Class.class) { + throw new IllegalAccessException( + "Can not call newInstance() on the Class for java.lang.Class" + ); + } + try { + Class[] empty = {}; + // 获取构造器 + final Constructor c = getConstructor0(empty, Member.DECLARED); + // Disable accessibility checks on the constructor + // since we have to do the security check here anyway + // (the stack depth is wrong for the Constructor's + // security check to work) + java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public Void run() { + c.setAccessible(true); + return null; + } + }); + cachedConstructor = c; + } catch (NoSuchMethodException e) { + throw (InstantiationException) + new InstantiationException(getName()).initCause(e); + } + } + Constructor tmpConstructor = cachedConstructor; + // Security check (same as in java.lang.reflect.Constructor) + int modifiers = tmpConstructor.getModifiers(); + if (!Reflection.quickCheckMemberAccess(this, modifiers)) { + Class caller = Reflection.getCallerClass(); + if (newInstanceCallerCache != caller) { + Reflection.ensureMemberAccess(caller, this, null, modifiers); + newInstanceCallerCache = caller; + } + } + // Run constructor + try { + // 调用构造器 + return tmpConstructor.newInstance((Object[])null); + } catch (InvocationTargetException e) { + Unsafe.getUnsafe().throwException(e.getTargetException()); + // Not reached + return null; + } +} +``` + +## getClassLoader + +```java +@CallerSensitive +public ClassLoader getClassLoader() { + ClassLoader cl = getClassLoader0(); + if (cl == null) + return null; + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass()); + } + return cl; +} +``` \ No newline at end of file diff --git a/second/week_06/87/Spring.md b/second/week_06/87/Spring.md new file mode 100644 index 0000000..5d41310 --- /dev/null +++ b/second/week_06/87/Spring.md @@ -0,0 +1,53 @@ +# Spring + +![image-20200413000432247](/Users/zain/Library/Application Support/typora-user-images/image-20200413000432247.png) + +## Core + +### core + +#### IoC&DI + +- 控制反转容器。 + +- BeanFactory + +### beans + +spring-beans负责实现Spring框架的IOC模块 + +### context + +ApplicationContext + +### el + + + +## support + +### AOP + +- 面向切面的编程思想 + +Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。 +Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。 +Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。 +Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。 +Target(目标对象):织入 Advice 的目标对象.。 +Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程 + +### ORM + +对象关系映射,是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.从效果上说,它其实是创建了一个可在编程语言里使用的--"虚拟对象数据库". + + + +三个原则: + +- 简单:以最基本的形式建模数据 +- 传达性:数据库结构被任何人都能理解的语言文档化 +- 精确性:基于数据模型创建正确标准化的结构 + +## web + -- Gitee