【JDK学习篇】LinkedList模仿

java中LinkedList的实现,看看Josh Bloch写的源码才发现真正大师的作品,真的是佩服的五体投地
josh Bloch
如图就是josh Bloch,自己还买过他的《Efficient java》,不过至今还躺在自己的书架上。
这次看看jdk中的源码从中学习,发现他们写的东西真的注意到很多,不论是安全,还是快捷方面都是自己要学习的。

模仿心得体会

  • 在学习源码时,便对着高淇的java300集学,然后就是打开源码自己模仿,在编写过程中也出现过许多错误,然后就是对着他的源码抄,看看他的设计的方法,他的怎么实现。关于代码复用源码做到的很详细,把一些小的功能拧出来。使代码更加简洁有力,内聚性很强,耦合度低。
  • 在获取指定位置结点时,josh Bloch根据index < (size >> 1)判断,从而缩小范围,更加迅速。

josh Bloch代码使用泛型,内部Node类内实现数据更好的隐藏,如:
Node类
仔细一看才发现大师写的那才是艺术,给人美的享受。
记得曾经读过的一片文章写到好的代码就是读的时候骂娘的次数,读JDK源码我真的是很折服,完全被它的代码所吸引。

不足之处

没有使用泛型,和内部类。另外还有一些细节还没有全部展开
比如源码中的transient、Serializable(串行化)还没有用上。
LinkedList

package com.huat.container;

import java.util.List;

class Node {
    private Node front;
    private Object obj;
    private Node next;

    public Node() {

    }

    public Node getFront() {
    return front;
    }

    public Node(Node front, Object obj, Node next) {
    super();
    this.front = front;
    this.obj = obj;
    this.next = next;
    }

    public void setFront(Node front) {
    this.front = front;
    }

    public Object getObj() {
    return obj;
    }

    public void setObj(Object obj) {
    this.obj = obj;
    }

    public Node getNext() {
    return next;
    }

    public void setNext(Node next) {
    this.next = next;
    }

}

public class ArrayList {

    /*********************************************
     * LinkList java实现双向链表
     * 
     **********************************************/
    private Node first;
    private Node last;
    private int size;

    /**
     * node结点插入数据
     */
    public void add(Object obj) {

    Node node = new Node(null,obj,null);

    if (first == null) {
        first = node;
        last = node;
    } else {
        last.setNext(node);
        node.setFront(last);
        last = node;
    }
    size++;
    }

    public int getSize() {
    return size;
    }

    private boolean isElementIndex(int index) {
    return index >= 0 && index < size;
    }

    private String outBoundsMsg(int index) {
    return "index " + index + ", size " + size;
    }

    private void checkElementIndex(int index) {
    // 如果下标越界则抛出异常
    if (!isElementIndex(index)) {
        throw new IndexOutOfBoundsException(outBoundsMsg(index));
    }
    }

    private Node node(int index) {
    // 使用二分查找
    if (index < (size >> 1)) {
        Node x = first;
        for (int i = 0; i < index; i++) {
        x = x.getNext();
        }
        return x;
    } else {
        Node x = last;
        for (int i = size - 1; i > index; i--) {
        x = x.getFront();
        }
        return x;
    }
    }

    public Object get(int index) {
    // 检查合法性
    checkElementIndex(index);
    return node(index).getObj();
    }

    public Object unlink(Node n) {
    Object o = n.getObj();
    Node front = n.getFront();
    Node next = n.getNext();

    if (front == null) {
        first = next;
    } else {
        front.setNext(next);
        n.setFront(null);       //释放n结点的前驱
    }

    if (next == null) {
        last = front;
    } else {
        next.setFront(front);
        n.setNext(null);        //释放n结点的后继结点
    }
    n.setObj(null);
    size--;
    return o;
    }

    public Object remove(int index) {
    checkElementIndex(index);
    Object o = unlink(node(index));
    return o;
    }

    public boolean remove(Object o) {
    // 从起始位置遍历链表,如果找到第一个相同的obj就直接删除
    Node temp = first;
    if (o == null) {
        while (temp.getNext() != null) {
        if (temp.getObj() == null) {
            unlink(temp);
            return true;
        }
        temp = temp.getNext();
        }
    } else {
        while (temp.getNext() != null) {
        if (o.equals(temp.getObj())) {
            unlink(temp);
            return true;
        }
        temp = temp.getNext();
        }
    }
    return false;
    }

    private void linkLast(Object o) {
    Node l = last;
    Node n = new Node(l,o,null);
    if (l == null) {
        last = n;
    } else {
        l.setNext(n);
    }
    size++;
    }

    private void linkBefore(Object o,Node point) {
    Node prev = point.getFront();
    Node newNode = new Node(prev, o, point);
    point.setFront(newNode);    
    if (prev == null) {
        first = newNode;
    } else {
        prev.setNext(newNode);
    }
    size++;
    }

    public void add(int index, Object o) {
    if (index > size) {
        throw new IndexOutOfBoundsException("index " + index + "超出 size " + size);
    }

    if (index == size) {
        linkLast(o);
    } else {
        linkBefore(o, node(index));
    }

    }

    public void print() {
    Node temp = first;
    do {
        System.out.print(temp.getObj() + " ");
        temp = temp.getNext();
    } while (temp != null);

    }

    public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("aa");
    list.add("bb");
    list.add("cc");
    list.add("bb");
    list.add("dd");

    list.add(5,"ee");

    // Test
    System.out.println("链表中的值为 :");
    list.print();   //遍历链表
    System.out.println();

    System.out.println("移除制定对象后的链表");
    System.out.println(list.remove("aa"));  //移除制定位置元素
    list.print();

    System.out.println();
    System.out.println(list.getSize());     //获取链表大小
    System.out.println(list.get(2));    //获取特点位置元素
    System.out.println(list.remove(0)); //移除制定位置元素
    list.print();
    System.out.println(list.getSize());
    System.out.println(list.get(0));

    }

}

运行结果

这里写图片描述

猜你喜欢

转载自blog.csdn.net/alearn_/article/details/80382141