JAVA SE ArrayList 底层实现

Posted Steve Yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA SE ArrayList 底层实现相关的知识,希望对你有一定的参考价值。

Array 查询效率高,增删效率低(

Link 增删效率高

Vector 线程安全

List 列表

源代码:

package com.littlepage.test;

/**
 * 基于底层实现ArrayList
 * @author Littlepage
 * @reference bjsxt.com
 */
public class LittlePagesArrayList<E> {
    // the array of element
    private Object[] elementData;
    private int size;// the size of SxtArrayList

    private static final int DEFALT_CAPACITY = 10;// the initial capacity

    public LittlePagesArrayList() {
        elementData = new Object[DEFALT_CAPACITY];
    }

    public LittlePagesArrayList(int capacity) {// set the capacity of SxtArrayList
        if (capacity < 0) {
            throw new RuntimeException("the capacity is negative");
        } else if (capacity == 0) {
            elementData = new Object[DEFALT_CAPACITY];
        } else {
            elementData = new Object[capacity];
        }
    }

    public void add(E element) {
        if (elementData.length == size) {
            /**
             * enlarge the capacity by 1.5 times
             **/
            Object[] newArray = new Object[elementData.length
                    + (elementData.length >> 1)];
            // use the system method copy the array
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }
        elementData[size++] = element;
    }

    public E getElement(int index) {
        checkRange(index);
        return (E) elementData[index];// get element by index
    }

    public void setElement(int index, E element) {
        /**
         * set element by index
         */
        checkRange(index);
        elementData[index] = element;
    }

    public void removeElement(int index) {
        /**
         * remove by index
         */
        int numMoved=elementData.length-index-1;
        if(numMoved>0){
            System.arraycopy(elementData, index+1, elementData, index, numMoved);
        }
        elementData[size-1]=null;
        size--;
    }

    public void removeElement(E element){
        /**
         * remove by element
         */
        Object objElement=(Object)element;
        for (int i=0;i<size;i++) {
            if(objElement.equals(elementData[i])){
                removeElement(i);//remove by index
            }
        }
    }

    public void checkRange(int index) {
        /**
         * check the index range
         */
        if (index >= size || index < 0) {
            throw new IndexOutOfBoundsException(
                    "the index of SxtArrayList is out of Bounds:" + index);
        }
    }
    
    public int size(){
        return size;
    }
    
    public boolean isEmpty(){
        return size==0;
    }
    @Override
    public String toString() {// the toString method
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i=0;i<size;i++) {
            sb.append(elementData[i] + ",");
        }
        sb.setCharAt(sb.length() - 1, ]);
        return sb.toString();
    }

    /**
     * test method
     * 
     * @param args
     */
    public static void main(String[] args) {
        LittlePagesArrayList s1 = new LittlePagesArrayList(20);
        System.out.println(s1.isEmpty());
        for (int i = 0; i < 32; i++) {
            s1.add("littlepage"+i);
        }
        s1.removeElement(6);
        s1.removeElement(5);
        s1.removeElement("littlepage0");
        System.out.println(s1.getElement(5));
        System.out.println(s1);
        System.out.println(s1.size());
        System.out.println(s1.isEmpty());
    }

}

 

以上是关于JAVA SE ArrayList 底层实现的主要内容,如果未能解决你的问题,请参考以下文章

ArrayList底层代码日记

集合框架

JAVA学习笔记-模拟ArrayList容器的底层实现

Java集合之ArrayList源码解析

Java ArrayList底层实现原理源码详细分析Jdk8

java Proxy 底层实现