自定義數組列表和隊列
阿新 • • 發佈:2017-09-03
blue 是否 sta entry builder live 長度 while else
最近一直在研究數據結構與算法,涉及到自定義數組和隊列,感覺對JDK源代碼的底層功能實現學習有一定的幫助,故作此總結,以供參考。
ps:JDK的源代碼更加復雜,我的自定義數組列表和隊列只是一些簡單的邏輯實現。
1.自定義數組列表(MyArrayList.java)
package com.BlueStarWei.arrayList; /** * * show all fields and method,please click "ctrl + o" * * * 開發過程遇到的問題: * 1. 傳入下標(index)未考慮越界情況 * 一定要校驗指針是否越界 * 2. 當容量耗盡後,如何拓展新的空間 * 添加一個變量(hasInitCapacity),用於判斷數組的創建方式。 * 如果是通過無參構造方式創建(hasInitCapacity = false),每次擴展容量的時候在原有數組長度的基礎上增加拓展長度。 * *@author HuWei * * @param <E> */ public class MyArrayList<E> { /** * The default capacity while use constructor form superClass */ private final int DEFUALT_CAPACITY = 10; /** * The expending capacity while arr that is generated via constructor form superClass is full*/ private final int ADD_CAPACITY = DEFUALT_CAPACITY / 2; /** * The array to store elements */ private E[] arr; /** * The length of element to be stored into arr */ private int size; /** * check whether is the queue generated by constructor with initCapacity*/ private boolean hasInitCapacity; public MyArrayList() { arr = (E[]) new Object[DEFUALT_CAPACITY]; hasInitCapacity = false; } public MyArrayList(int initCapacity){ arr = (E[]) new Object[initCapacity]; hasInitCapacity = true; } /** * * @return * the length of array */ public int size() { return size; } /** * let element insert to arr * if MyArrayList is generated via constructor form superClass and the capacity is full,then expand capacity * @param element * the element to insert * @return index * the index of the element to insert */ public int add(E element){ //expend capacity if(!hasInitCapacity && size == arr.length){ E[] arr2 = arr; arr = (E[]) new Object[arr.length + ADD_CAPACITY]; for (int i = 0; i < arr2.length; i++) { arr[i] = arr2[i]; } } arr[size] = element; return size++; } /** * * @param index * the index of the element to replace * @param element * the element to replace * @return * the element to be replaced */ public E update(int index, E element){ checkArange(index); E old = arr[index]; arr[index] = element; return old; } /** * * @param index * the index of the element to be removed * @return * the element to be removed */ public E remove(int index){ checkArange(index); E element = arr[index]; if(index == size - 1){ arr[index] = null; } for (int i = index; i < size; i++) { arr[i] = arr[i+1]; } size--; return element; } /** * * @param index * the index of the element to get * @return * the element to get */ public E get(int index){ return arr[index]; } /** * check whether index is out of bounds * @param index */ private void checkArange(int index){ if(index < 0 || index >= size){ throw new ArrayIndexOutOfBoundsException(); } } @Override public String toString() { StringBuilder result = new StringBuilder(); result.append("["); for (int i = 0; i < size; i++) { result.append(arr[i]); if(i != size - 1){ result.append(","); } } result.append("]"); return result.toString(); } }
2. 自定義隊列(MyQueue.java)
package com.BlueStarWei.queue; /** * 開發終於到的問題: * 1. 元素poll完(元素實際上並沒有從數組中刪除,只是將頭指針向後移)之後,再次添加數據的時候如何從頭開始? * 判斷queue有效的元素是否為空,如果為空,將頭指針和尾指針重置到數組最前端 * * 優化: * 1. 與自定義數組列表相比,在創建隊列的時候沒有隊列的類型為任意集合(E)類型,而是指定為Object[]類型, * 只有在元素返回時,返回為任意集合(E)類型 * * @author HuWei * * @param <E> */ public class MyQueue<E> { /** * The default capacity while use constructor form superClass */ private final int DEFUALT_CAPACITY = 10; /** * The expending capacity while arr that is generated via constructor form superClass is full */ private final int ADD_CAPACITY = DEFUALT_CAPACITY / 2; /** * store the element */ private Object[] queue; /** * the head pointer of queue *it always point the first alive element */ private int front; /** * the end pointer of queue * it always point the last alive element */ private int end; /** * the real size of queue */ private int size; /** * check whether is the queue generated by constructor with initCapacity */ private boolean hasInitCapacity; /** * generate a queue whose default size is DEFUALT_CAPACITY */ public MyQueue() { queue = new Object[DEFUALT_CAPACITY]; hasInitCapacity = false; end = -1; } /** * generate a queue whose default size is initCapacity */ public MyQueue(int initCapacity){ queue = new Object[initCapacity]; hasInitCapacity = true; end = -1; } /** * store element to queue * @param element * the element to be stored */ public void push(E element){ if(!hasInitCapacity && size == queue.length){ Object[] queue2 = queue; queue = new Object[size + ADD_CAPACITY]; for (int i = 0; i < queue2.length; i++) { queue[i] = queue2[i]; } } queue[++end] = element; size++; } /** * remove the first alive element in queue * while queue is queue is empty, set front and end pointer point the queue‘s head * * @return * the element to be remove */ public E poll(){ E element = (E)queue[front++]; size--; if(isEmpty()){ front = 0; end = -1; } return element; } /** * get the first alive element in queue * @return * the element to be returned */ public E peek(){ E element = (E)queue[front]; if(isEmpty()){ element = null; } return element; } /** * check whether is queue empty. * * @return * */ public boolean isEmpty(){ if(size == 0){ return true; }else { return false; } } /** * get the size of queue * * @return * the size of queue */ public int size(){ return size; } }
更多內容,請訪問http://www.cnblogs.com/BlueStarWei
自定義數組列表和隊列