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底层实现的更多相关文章

  1. ArrayList底层实现

    ArrayList 底层是有数组实现,实际上存放的是对象的引用,而不是对象本身.当使用不带参的构造方法生成ArrayList对象时,实际会在底层生成一个长度为10的数组 当添加元素超过10的时候,会进 ...

  2. ArrayList底层原理

    ArrayList底层采用数组实现,访问特别快,它可以根据索引下标快速找到元素.但添加插入删除等写操作效率低,因为涉及到内存数据复制转移. ArrayList对象初始化时,无参数构造器默认容量为10, ...

  3. JAVA容器-模拟ArrayList的底层实现

    概述 ArrayList实质上就是可变数组的实现,着重理解:add.get.set.remove.iterator的实现,我们将关注一下问题. 1.创建ArrayList的时候,默认给数组的长度设置为 ...

  4. ArrayList底层实现原理

    ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括null在内的所有元素.除了实现列表接口外,此类还提供一些方法来操作内部用来存储列表的数组 ...

  5. ArrayList 底层实现原理

    ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...

  6. JAVA SE ArrayList 底层实现

    Array 查询效率高,增删效率低( Link 增删效率高 Vector 线程安全 List 列表 源代码: package com.littlepage.test; /** * 基于底层实现Arra ...

  7. Java——ArrayList底层源码分析

    1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数 ...

  8. ArrayList底层代码解析笔记

    通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...

  9. 模拟ArrayList

    package com.helloidea; import java.util.ArrayList; import java.util.Collection; import java.util.Lis ...

随机推荐

  1. &quot;高可用方案工具包&quot; high availability toolkit 1.1

    "高可用方案工具包" high availability toolkit 1.1 公布了. version 1.1 新增了gossip protocol 的高可用HA方案应用. 项 ...

  2. [TB-Technology] 淘宝在数据处理领域的项目及开源产品介绍

    淘宝在数据存储和处理领域在国内互联网公司中一直保持比较靠前的位置,而且由于电子商务领域独特的应用场景,淘宝在数据实时性和大规模计算及挖掘方面一直在国内保持着领先,因此积累了很多的实践的经验和产品. T ...

  3. Spring生态简介

    目录 概述 项目说明 主要项目 社区项目 保留项目 最后总结 概述 做Java开发的人一提起Spring,首先在脑海中浮现出的就是"IoC","AOP",&qu ...

  4. A - I Wanna Be the Guy

    Problem description There is a game called "I Wanna Be the Guy", consisting of n levels. L ...

  5. [] == ![]为什么是true

    我们先来考虑这个问题,console.log([] == false)会打印什么呢? 答案是true.为什么呢? 首先,因为当"=="号两边其中一个是布尔值的话,先把它转换为数字( ...

  6. VM虚拟机NAT模式主机与虚拟机ping不通解决方案

    VM虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST-ONLY 模式. NAT模式 使用虚拟机的一个虚拟网卡做NAT网关,在nat网关上配dhcp ,或者直接用静态地址.就相当于形成了一 ...

  7. sqoop从DB2迁移数据到HDFS

    Sqoop import job failed to read data from DB2 database which has UTF8 encoding. Essentially, even th ...

  8. 使用meta实现页面的定时刷新或跳转

    <meta http-equiv="refresh" content="5"> 这个表示当前页面每5秒钟刷一下,刷一下~ <meta http ...

  9. jQuery访问json文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. js 读取外部的本地json文件

    Javascript 读取外部的本地json文件 方案1 运行本地web服务器,提供文件服务 方案2 1.data = '[{"name" : "Ashwin" ...