模拟ArrayList底层实现

Posted

tags:

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

package chengbaoDemo;
 
import java.util.ArrayList;
import java.util.Arrays;

import comman.Human;
/**
 * ArrayList 底层实现
 */
public class MyArrayList {
    /**
     * The value is used for Object Stroage.
     */
    private Object value[];

    /**
     *The size is the number of Object used. 
     */

    private int size;
    public MyArrayList() {
//        value = new Object[10];
        this(10);
    }
    
    public MyArrayList(int size) {
        value = new Object[size];
    }
    
    /**
     *Get the number of array‘s element  
     */
    public int size() {
        return size;
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    /**
     *add element into the object storage. 
     */
    public void add(Object obj) {
        value[size] = obj;
        size++;
        //扩容
        if (size >= value.length) {
            ensureCapacity();
        }
    }
    /**
     *扩容 
     */
    public void ensureCapacity() {
        int newLength = value.length * 2 + 2;
        
        Object newObj[] = Arrays.copyOf(value, newLength);
        
        value = newObj;
        
    }
    
    
    /**
     *Get the element from the object storage. 
     */
    public Object get(int size) {
        rangeCheck(size);
        
        return value[size];
    }
    /**
     * Check whether occured  out of bound Exception
     */ 
    public void  rangeCheck(int index) {
        if (index < 0 || index > value.length) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }  
    }
    
    /**
     * Return the index of the first occurrence of the specfied element in this value,
     * or -1 if the value does not contains the specfied element.
     */
    public int indexOf(Object obj) {
        if (obj == null) {
            for (int i = 0 ; i < value.length; i++) {
                if (value[i] == null) {
                    return i;
                }
            }
            return -1;

        }else {
            for (int i = 0; i < value.length; i++) {
                if (value[i].equals(obj)) {
                    return i;
                }

            }
            return -1;
        }
    }
    
    
    /**
     *Repaces the element at the specfied position in this object array 
     *with the specfied element.
     */
    public Object set(int index, Object obj) {
        rangeCheck(index);
        Object oldObj = value[index];
        value[index] = obj;
        return oldObj;
        
    }


    public void printf() {
        for (int i = 0; i < size; i++) {
            System.out.println(value[i]);
        }
    }
    ///测试
    public static void main(String[] args) {
        MyArrayList mal = new MyArrayList(3);
        mal.add("asd");
        mal.add("qwe");
        mal.add("asd");
        mal.add("qwe");
        Human h = new Human("成宝");
        mal.add(h);
        System.out.println(mal.size());
        
        Human hs = (Human)mal.get(4);
        System.out.println(hs.getName());
        mal.add(null);
        System.out.println(mal.get(5));
        System.out.println(mal.indexOf(null));
        
        mal.printf();
    
        mal.set(5, 90);
        
        mal.printf();

    }
    
    
}

 

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

JAVA——底层源码阅读——集合ArrayList的实现底层源码分析

JAVA SE ArrayList 底层实现

ArrayList底层代码日记

你真的知道ArrayList的使用和实现吗?

ArrayList的底层数组扩容你会吗?

ArrayList底层源码实现练习