模拟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();

    }

}
时间: 2024-08-07 00:15:13

模拟ArrayList底层实现的相关文章

模拟 extjs 底层继承

1.混合继承的弊端 混合继承在继承原型的时候,其实将 父类的模板 再次继承,影响效率 // 混合继承 function Person(name,age) { this.name = name; this.age = age; } Person.prototype = { constructor : Person, sayHello : function () { alert("hello"); } } function Boy(name,age,sex) { //Person.call

ArrayList底层原理

ArrayList底层采用数组实现,访问特别快,它可以根据索引下标快速找到元素.但添加插入删除等写操作效率低,因为涉及到内存数据复制转移. ArrayList对象初始化时,无参数构造器默认容量为10,当空间不足时会扩容,扩容后的容量是老容量的1.5倍.Java8的ArrayList源代码第259行,可以看到将原始容量数右移一位,即每次扩充老容量的二分之一,即新增0.5倍,换句话说新容量是老容量的1.5倍. 原文地址:https://www.cnblogs.com/huigee/p/9725256

手撕ArrayList底层,透彻分析源码

ArrayList概述 Hello大家好,今天就来介绍一下ArrayList,说到ArrayList,很多人都知道它的底层是使用数组实现的,线程不安全的,说到它的特点,都会说查找快,增删慢,因为面试题大家都是这么背过来的.今天就来说说它的底层源码吧. ArrayList更准确的说是动态数组去实现的,这里使用动态两字,是为了能够充分体现它的特点. 再者就是ArrayList不是线程安全的,所以效率比较高,但是否这个是绝对的呢?答案是否定的 . ArrayList底层源码 public class

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

package MyArrayList;import java.util.*;/** * 模拟实现JDK中的ArrayList类 * @author iwang * */public class MyArrayList { /** * The value is used for Object storage. */ private Object[] value; /** * The size is the number of Object used. */ private int size; p

ArrayList底层代码日记

通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable 由此可见,ArrayList继承自AbastractList,以及实现了以上四个接口; public abstract class AbstractList<E> extends Abstract

模拟jQuery底层源码的链式调用和常用的$()方法的实现

最近在看jQuery框架的源码,感觉还是学到不少东西的,所以就想总结一下自己的知识,和广大的前端爱好者一起 交流一下,我下面所说的并不是直接对jQuery的源码来解读,我是模拟一下jQuery底层源码的链式调用大概是怎么 实现的和常用的$功能是怎么实现的.好了废话不多说了.你要看这个,你就要对jQuery有一定的了解,最起码你要用过jQuery.首先看下jQuery的源码开始是怎么写的 (function( window, undefined){    })(window);它的代码就是被这个块

ArrayList底层源码实现练习

/** * Created by chengbx on 2018/5/17. * 自己实现一个ArrayList,帮助我们更好的理解ArrayList的底层结构! * 一句话概括ArrayList的底层:数组的扩容与数据的拷贝! */ public class CbxArrayList { //存储集合中的元素 private Object[] elementData; //容器中存储的元素数量 private int size; public CbxArrayList(){ this(10);

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; pri

Java——ArrayList底层源码分析

1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中. 当从 ArrayList 的中间位置插入或者删除元素时,需要对数组进行复制.移动.代价比较高.因此,它适合随机查找和遍历,不适合插入和删除. 线性表的顺序存储,插入删除元素的时间复杂度为O(n),求表长以及增加元素,取第 i 元素的时间复杂度为O(1). ArrayL