码迷,mamicode.com
首页 > 其他好文 > 详细

模拟ArrayList底层实现

时间:2016-02-07 13:34:30      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

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底层实现

标签:

原文地址:http://www.cnblogs.com/chengbao/p/5184601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!