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开发:蓝牙聊天室APP(一)

    零基础学Android开发:蓝牙聊天室APP第一讲 1. Android介绍与环境搭建:史上最高效Android入门学习 1.1 Google的大小战略 1.2 物联网与云计算 1.3 智能XX设备 ...

  2. Tomcat启动时项目反复载入,导致资源初始化两次的问题

    近期在项目开发測试的时候,发现Tomcat启动时项目反复载入,导致资源初始化两次的问题  导致该问题的解决办法: 例如以下图:在Eclipse中将Server Locations设置为"Us ...

  3. LeetCode OJ 之 Delete Node in a Linked List (删除链表中的结点)

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  4. luogu1890 gcd区间

    题目大意:给定一行n个正整数a[1]..a[n].m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 因为gcd满足交换律和结合律,所以用线段树维护区间上的gcd值即可. ...

  5. hdoj--2151--Worm(dp)

    Worm Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. The Triangle--nyoj 18

    The Triangle 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...

  7. Spring进行表单验证

    转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...

  8. 使用C#正则表达式获取必应每日图片地址

    微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错. 首先访问微软的API,该地址返回的 ...

  9. 解决Android单个dex文件不能超过65535个方法问题

    一.找坑:谷歌规定单个dex文件中的方法不能超过65536的限制 我们编写项目过程中在工程的lib文件夹下引用的第三方插件jar包太多或者项目过大,编译运行时就有可能报出com.android.dex ...

  10. linux下使用Android studio启动模拟器时提示 waiting for target device to come online 的问题

    方法来自:http://stackoverflow.com/questions/42612468/how-can-i-get-more-information-about-waiting-for-ta ...