diff --git "a/second/week_01/78/ArrayList \346\272\220\347\240\201\345\210\206\346\236\220.md" "b/second/week_01/78/ArrayList \346\272\220\347\240\201\345\210\206\346\236\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..17215525f25d6e7c38e56b231725b296e83aeed5 --- /dev/null +++ "b/second/week_01/78/ArrayList \346\272\220\347\240\201\345\210\206\346\236\220.md" @@ -0,0 +1,243 @@ +# ArrayList 源码分析 +## 类关系 +- [ ] 继承 AbstractList +- [ ] 实现 List、 RandomAccess、CloneAble、Serializable + +## 类常量 +- [ ] DEFAULT_CAPACITY = 10; 默认初始化数组长度 +- [ ] EMPTY_ELEMENTDATA = {};空数组 +- [ ] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; 默认空对象数组 + +## 属性 +- [ ] size 长度 + +## 常用方法 +### contains 方法 +```java +public boolean contains(Object o) { + return indexOf(o) >= 0; +} +``` +调用 indexOf 方法,判断是否包含某个元素,int 返回值,包含返回第一个下标,没有则返回 -1 ,可以传入 null,最后返回 boolean + +### lastIndexOf 方法 +```java +public int lastIndexOf(Object o) { + if (o == null) { + for (int i = size-1; i >= 0; i—) + if (elementData[i]==null) + return i; + } else { + for (int i = size-1; i >= 0; i—) + if (o.equals(elementData[i])) + return i; + } + return -1; +} +``` + 倒序遍历,返回下标,没有则返回 -1 + +### toArray 方法 +```java +public Object[] toArray() { + return Arrays.copyOf(elementData, size); +} +``` +调用 Arrays.copyOf + +### get 方法 +``` +public E get(int index) { + rangeCheck(index); + + return elementData(index); +} + +``` +首先调用 rangeCheck 方法,检查是否越界,越界直接抛 IndexOutOfBoundsException 异常,通过 elementData 方法返回指定值 + +### set 方法 +``` +public E set(int index, E element) { + rangeCheck(index); + + E oldValue = elementData(index); + elementData[index] = element; + return oldValue; +} +``` +修改指定值,检查下标是否越界, 获取旧的值,设置新值,返回旧值 + +### add 方法 +``` +/** + * Appends the specified element to the end of this list. + * + * @param e element to be appended to this list + * @return true (as specified by {@link Collection#add}) + */ +public boolean add(E e) { + ensureCapacityInternal(size + 1); // Increments modCount!! + elementData[size++] = e; + return true; +} + +/** + * Inserts the specified element at the specified position in this + * list. Shifts the element currently at that position (if any) and + * any subsequent elements to the right (adds one to their indices). + * + * @param index index at which the specified element is to be inserted + * @param element element to be inserted + * @throws IndexOutOfBoundsException {@inheritDoc} + */ +public void add(int index, E element) { + rangeCheckForAdd(index); + + ensureCapacityInternal(size + 1); // Increments modCount!! + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = element; + size++; +} + +``` + 添加某个元素,动态扩容,第二个 add 方法是指在指定位置插入一个元素,不是修改 + +### remove 方法 +``` +/** + * Removes the element at the specified position in this list. + * Shifts any subsequent elements to the left (subtracts one from their + * indices). + * + * @param index the index of the element to be removed + * @return the element that was removed from the list + * @throws IndexOutOfBoundsException {@inheritDoc} + */ +public E remove(int index) { + rangeCheck(index); + + modCount++; + E oldValue = elementData(index); + + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + elementData[—size] = null; // clear to let GC do its work + + return oldValue; +} + +/** + * Removes the first occurrence of the specified element from this list, + * if it is present. If the list does not contain the element, it is + * unchanged. More formally, removes the element with the lowest index + * i such that + * (o==null ? get(i)==null : o.equals(get(i))) + * (if such an element exists). Returns true if this list + * contained the specified element (or equivalently, if this list + * changed as a result of the call). + * + * @param o element to be removed from this list, if present + * @return true if this list contained the specified element + */ +public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } else { + for (int index = 0; index < size; index++) + if (o.equals(elementData[index])) { + fastRemove(index); + return true; + } + } + return false; +} + + +``` +一个基于元素删除,一个基于下标删除 + +### clear +``` +/** + * Removes all of the elements from this list. The list will + * be empty after this call returns. + */ +public void clear() { + modCount++; + + // clear to let GC do its work + for (int i = 0; i < size; i++) + elementData[i] = null; + + size = 0; +} + +``` +清除方法,无返回值 + +### addAll +``` +/** + * Appends all of the elements in the specified collection to the end of + * this list, in the order that they are returned by the + * specified collection’s Iterator. The behavior of this operation is + * undefined if the specified collection is modified while the operation + * is in progress. (This implies that the behavior of this call is + * undefined if the specified collection is this list, and this + * list is nonempty.) + * + * @param c collection containing elements to be added to this list + * @return true if this list changed as a result of the call + * @throws NullPointerException if the specified collection is null + */ +public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int numNew = a.length; + ensureCapacityInternal(size + numNew); // Increments modCount + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + return numNew != 0; +} + +/** + * Inserts all of the elements in the specified collection into this + * list, starting at the specified position. Shifts the element + * currently at that position (if any) and any subsequent elements to + * the right (increases their indices). The new elements will appear + * in the list in the order that they are returned by the + * specified collection’s iterator. + * + * @param index index at which to insert the first element from the + * specified collection + * @param c collection containing elements to be added to this list + * @return true if this list changed as a result of the call + * @throws IndexOutOfBoundsException {@inheritDoc} + * @throws NullPointerException if the specified collection is null + */ +public boolean addAll(int index, Collection c) { + rangeCheckForAdd(index); + + Object[] a = c.toArray(); + int numNew = a.length; + ensureCapacityInternal(size + numNew); // Increments modCount + + int numMoved = size - index; + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, index + numNew, + numMoved); + + System.arraycopy(a, 0, elementData, index, numNew); + size += numNew; + return numNew != 0; +} + +``` +批量追加,指定位置批量追加,需要动态扩容数组 \ No newline at end of file diff --git "a/second/week_01/78/LinkList \346\272\220\347\240\201\345\210\206\346\236\220.md" "b/second/week_01/78/LinkList \346\272\220\347\240\201\345\210\206\346\236\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..f157c434a73c4816f40fe8dacf7eb36fea6d607a --- /dev/null +++ "b/second/week_01/78/LinkList \346\272\220\347\240\201\345\210\206\346\236\220.md" @@ -0,0 +1,118 @@ +# LinkList 源码分析 +## 类关系 +- [ ] 继承 AbstractSequentialList +- [ ] 实现 List, Deque, Cloneable, Serializable + +## 类属性 +- [ ] size:长度 +- [ ] first :第一个节点 +- [ ] last:最后一个节点 + +## 常用方法 +### NODE + +``` +private static class Node { + E item; + Node next; + Node prev; + + Node(Node prev, E element, Node next) { + this.item = element; + this.next = next; + this.prev = prev; + } +} +``` + +item 内容, next 下一个节点, prev 上一个节点, + +### getFirst +``` +/** + * Returns the first element in this list. + * + * @return the first element in this list + * @throws NoSuchElementException if this list is empty + */ +public E getFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return f.item; +} + +``` +获取第一个 node ,没有就抛异常,有则返回内容 +### getLast +``` +/** + * Returns the last element in this list. + * + * @return the last element in this list + * @throws NoSuchElementException if this list is empty + */ +public E getLast() { + final Node l = last; + if (l == null) + throw new NoSuchElementException(); + return l.item; +} + + +``` +获取最后一个节点 +### unlink +``` +/** + * Unlinks non-null node x. + */ +E unlink(Node x) { + // assert x != null; + final E element = x.item; + final Node next = x.next; + final Node prev = x.prev; + + if (prev == null) { + first = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.item = null; + size—; + modCount++; + return element; +} + +``` +删除指定节点,注意需要 特别维护 prev && next +### indexOf +``` +public int indexOf(Object o) { + int index = 0; + if (o == null) { + for (Node x = first; x != null; x = x.next) { + if (x.item == null) + return index; + index++; + } + } else { + for (Node x = first; x != null; x = x.next) { + if (o.equals(x.item)) + return index; + index++; + } + } + return -1; +} +``` +遍历获取,指定元素,从头开始,时间复杂度 O(n) \ No newline at end of file