java線性表之順序表實現,java線性順序實現

來源:互聯網
上載者:User

java線性表之順序表實現,java線性順序實現

仿照arrayList寫了一個簡化版的線性表,主要為了用來研究arrayList在實現什麼操作的情況下比較節省效能,樓主文采很差,直接上代碼.

import java.util.Arrays;public class SequenceList<T> {private final int DEFAULT_SIZE = 16;// 儲存數組的長度private int capacity;// 定義一個數組用於儲存順序線性表的元素private Object[] elementData;// 儲存順序表中元素的當前個數private int size = 0;// 以預設數組長度建立空順序線性表public SequenceList() {capacity = DEFAULT_SIZE;elementData = new Object[capacity];}// 以一個初始化元素建立順序線性表public SequenceList(T element) {this();elementData[0] = element;size++;}/** * 以指定長度的數組來建立順序線性表 *  * @param element *            指定順序線性表中第一個元素 * @param initSize *            指定順序線性表底層數組的長度 */public SequenceList(T element, int initSize) {capacity = 1;// 把capacity設為大於initSize的最小的2的n次方while (capacity < initSize) {capacity <<= 1;}elementData = new Object[capacity];elementData[0] = element;size++;}// 擷取順序線性表的大小public int length() {return size;}// 擷取順序線性表中索引為i處的元素public T get(int i) {if (i < 0 || i > size - 1) {throw new IndexOutOfBoundsException("線性表索引越界");}return (T) elementData[i];}// 尋找順序線性表中指定元素的索引public int locate(T element) {for (int i = 0; i < size; i++) {if (elementData[i].equals(element)) {return i;}}return -1;}// 向順序線性表的指定位置插入一個元素public void insert(T element, int index) {if (index < 0 || index > size) {throw new IndexOutOfBoundsException("線性表索引越界");}ensureCapacity(size + 1);// 將指定索引處之後的所有元素向後移動一格System.arraycopy(elementData, index, elementData, index + 1, size - index);elementData[index] = element;size++;}// 線上性順序表的開始處添加一個元素public void add(T element) {insert(element, size);}// 很麻煩,而且效能很差private void ensureCapacity(int minCapacity) {// 如果數組的原有長度小於目前所需的長度if (minCapacity > capacity) {// 不斷地將capacity * 2,直到capacity大於minCapacitywhile (capacity < minCapacity) {capacity <<= 1;}elementData = Arrays.copyOf(elementData, capacity);}}// 刪除順序線性表中指定索引處的元素public T delete(int index) {if (index < 0 || index > size - 1) {throw new IndexOutOfBoundsException("線性表索引越界");}T oldValue = (T) elementData[index];int numMoved = size - index - 1;if (numMoved > 0) {System.arraycopy(elementData, index + 1, elementData, index, numMoved);}// 清空最後一個元素elementData[--size] = null;return oldValue;}// 刪除順序線性表中最後一個元素public T remove() {return delete(size - 1);}// 判斷順序線性表是否為空白表public boolean empty() {return size == 0;}// 清空線性表public void clear() {// 將底層數組所有元素賦為nullArrays.fill(elementData, null);size = 0;}public String toString() {if (size == 0) {return "[]";} else {StringBuilder sb = new StringBuilder("[");for (int i = 0; i < size; i++) {sb.append(elementData[i].toString() + ", ");}int len = sb.length();return sb.delete(len - 2, len).append("]").toString();}}}

順序表使用數組儲存資料,所以對於隨機的訪問有很好的效能支援,不管是訪問線性表上的哪一個元素都可以直接使用elementData[i]直接得到,但是對於添加元素會很消耗效能,主要是在隨機插入元素的時候可能要將後面的元素整體向後移一位,還有數組長度不夠的時候需要建立原數組2倍的新數組然後將資料整體搬家到新數組,然後釋放掉原數組這兩點上非常消耗效能,所以arrayList的使用通常是沒有複雜的插入操作,更多的是對資料的取操作,而LinkedList(鏈表)在這些使用的效能方面正好相反.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.