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. 怎样预置Android 手机 APK

    预制APK有下面4种情况: 1, 怎样将带源代码的 APK 预置进系统? 2, 怎样将无源代码的APK预置进系统? 3, 怎样预置APK使得用户能够卸载,恢复出厂设置时不能恢复? 4, 怎样预置APK ...

  2. C++设计模式之状态模式(二)

    2.智能空调的设计与实现 某软件公司将开发一套智能空调系统: 系统检測到温度处于20---30度之间,则切换到常温状态:温度处于30---45度,则切换到制冷状态: 温度小于20度,则切换到制热状态. ...

  3. HDU 1576 A/B(扩展欧几里德变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但因为A非常大,我们仅仅给出n ...

  4. IOS UITextView光标位置在中间的问题

    在viewDidLoad中 if ([selfrespondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) { se ...

  5. Linux下用ImageMagick玩图像魔术【转】

    本文转载自:http://www.linuxdiyf.com/linux/11680.html 不管你知不知道,现在是一个用ImageMagick的好机会,至少,如果你是一个Linux用户的话.这是一 ...

  6. express 4.2.0 使用session和cookies

    express 4.2.0 使用session和cookies https://my.oschina.net/u/1466553/blog/294336

  7. python 微信红包

    def redbags(money, num=10): import random choice = random.sample(range(1, money * 100), num - 1) cho ...

  8. Sybase 动态改变存储过程里查询的数据库

    declare @sql varchar(500) select @sql='select * from '+@dbName+'..tableName' --此句用于执行拼接好的SQL语句 exec( ...

  9. Linux目录结构(二)

    Linux文件系统结的结构是树形结构,其入口从/开始,了解Linux文件系统的结构,对于我们需要掌握的基础知识点之一. 2.文件系统的组织结构简说: 当您使用Linux的时候,如果您通过ls -la ...

  10. (转)50 个 jQuery 小技巧

    1. 如何修改jQuery默认编码(例如默认UTF-8改成改GB2312): $.ajaxSetup({ajaxSettings:{ contentType:"application/x-w ...