Learning java: java container --List interfaces (analog underlying code implementations ArrayList and LinkedList class)

java container --List Interface

1, the concept of

Data Interface List is ordered repeatable. Inherited the Collection interface.
There are three implementation class ArrayList, LinkedList, Vector.

ArrayList:
the underlying implementation is an array. Fast query, insert, delete slow. Thread-safe, high efficiency.
LinkedList:
the underlying implementation is a linked list. Slow query, insert, delete fast. Thread-safe, high efficiency.
Vector:
the underlying implementation is an array. Thread-safe, low efficiency. Generally when using multiple threads share, but generally is a local variable, it is generally not used.
Stack:
inherited the Vector class.

2, ArrayList underlying analog (underlying array)

Fast query : returns the object directly at the target storage array.
Insert, delete slow : array expansion would require the operation. And moving in the insertion and deletion need to copy the array to a new array, object-low efficiency.
( Expansion Mechanism: an array 10 of default length is automatically expanded to 1.5 times the original volume expansion)

/**
 * 自己实现一个ArrayList,帮助理解其底层实现;笔试一般写get,add,remove 底层通过数组实现。
 */
public class MyArrayList {
	private Object[] elementDate;//底层是数组
	private int size;//对象的个数
	public int size() {
		return size;
	}
	public MyArrayList() {
		this(10);//初始数组长度设为固定值
	}
	public MyArrayList(int initialCapacity) {//也可以在初始化ArrayList时就指定数组长度
		if (initialCapacity < 0) {
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		elementDate = new Object[initialCapacity];
	}
	/**
	 * 扩容
	 */
	private void extendCap() {
		if (size + 1 > elementDate.length) {//如果size+1超过数组长度
			Object[] newArray = new Object[size * 2 + 1];//先建立一个新的数组,昌都市现在size的两倍+1
			System.arraycopy(elementDate, 0, newArray, 0, elementDate.length);// 数组拷贝
			elementDate = newArray;//使源数组等于新建数组
		}
	}
/**
 * index处存放obj,数组后移
 * @param index
 * @param obj
 */
	public void add(int index, Object obj) {
		checkIndex(index);//检查index
		extendCap();//扩容
		System.arraycopy(elementDate, index, elementDate, index + 1, size
				- index );
		elementDate[index] = obj;
		size++;
	}

	public void add(Object obj) {
		// 应当先扩容
		extendCap();
		// 赋值
		elementDate[size] = obj;
		// 自增
		size++;

	}

	/**
	 * 判断是否为空
	 * @return
	 */
	public boolean isEmpty() {
		return size == 0;
	}

	public Object get(int index) {
		checkIndex(index);
		return elementDate[index];
	}

	public void set(int index, Object obj) {
		elementDate[index] = obj;
	}

	private void checkIndex(int index) {
		if (index <= size - 1 && index >= 0) {
		} else {
			throw new IndexOutOfBoundsException("index:" + index + "  size:"
					+ size);
		}

	}

	public void remove(Object obj) {
		for (int i = 0; i < size; i++) {
			if (get(i).equals(obj)) {// 注意底层调用的是equals方法而不是==
				remove(i);// 底层是只删除第一个
			}
		}
	}

	public void print() {
		for (int i = 0; i < size; i++) {
			System.out.print("  " + elementDate[i]);
		}
	}

	public void remove(int index) {
		checkIndex(index);
		int numMoved = size - index - 1;
		if (numMoved > 0) {
			System.arraycopy(elementDate, index + 1, elementDate, index,
					numMoved);
			elementDate[--size] = null;
		}
	}
}

3, LinkedList analog bottom (bottom list)

Slow query : query node needs to start from scratch, loop through the query object specified node.
Add, delete, fast : immediately before and after the change the node junction point without moving the data.

(1) a linked list node structure class

/**
 * 构造节点类
 * @author Linlin Zhao
 * 
 */
public class Node {

	Node previous;//前一个节点
	Object obj;//对象
	Node next;//下一个节点

	/**
	 * 构造器
	 * 
	 * @param previous
	 * @param obj
	 * @param next
	 */
	public Node(Node previous, Object obj, Node next) {
		super();
		this.previous = previous;
		this.obj = obj;
		this.next = next;
	}

	/**
	 * 构造器
	 */
	public Node() {

	}

}

(2) achieve LinkedList list

/**
 * 自己实现一个MyLinkedList,帮助理解其底层实现;一般考add和remove
 * 
 * @author Linlin Zhao
 * 
 */
public class MyLinkedList {
	private Node first;//头结点
	private Node last;//尾结点
	private int size;//节点个数

	public void add(Object obj) {
		Node n = new Node();
		if (first == null) {//若没有头结点,直接将对象放在头结点
			n.previous = null;
			n.obj = obj;
			n.next = null;
			first = n;
			last = n;
		} else {
			// 直接往last结点后加新的结点
			n.previous = last;
			n.next = null;
			n.obj = obj;
			last.next = n;
			last = n;
		}
		size++;
	}

	public int size() {
		return this.size;
	}

	public Object get(int index) {
		checkIndex(index);
		Node tempNode = node(index);
		return tempNode.obj;
	}

	private void checkIndex(int index) {
		if (index < size && index >= 0) {
		} else {
			throw new IndexOutOfBoundsException("index:" + index + "  size:"
					+ size);
		}

	}

	public void remove(int index) {
		checkIndex(index);
		Node tempNode = node(index);
		if (tempNode != null) {
			Node upNode = tempNode.previous;
			Node downNode = tempNode.next;
			upNode.next = downNode;
			downNode.previous = upNode;
		}
		size--;
	}

	/**
	 * 即通过节点遍历实现索引的功能
	 * 
	 * @param index
	 * @return
	 */
	private Node node(int index) {
		Node tempNode = null;
		if (first != null) {
			tempNode = first;
			for (int i = 0; i < index; i++) {
				tempNode = tempNode.next;
			}
		}
		return tempNode;
	}

	public void add(int index, Object obj) {
		checkIndex(index);
		Node tempNode = node(index);
		if (tempNode != null) {
			Node upNode = tempNode.previous;
			Node downNode = tempNode.next;
			Node add = new Node();
			add.next = downNode;
			add.obj = obj;
			add.previous = upNode;
			size++;
		}
	}
}

4, Vector underlying implementation

Vector and ArrayList underlying implementation is very similar, but also to achieve through the array. The biggest difference is that Vector is thread-safe, most methods are modified by synchronized.

Published 57 original articles · won praise 13 · views 1125

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105128912