模拟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(); } }
模拟ArrayList底层实现的更多相关文章
- ArrayList底层实现
ArrayList 底层是有数组实现,实际上存放的是对象的引用,而不是对象本身.当使用不带参的构造方法生成ArrayList对象时,实际会在底层生成一个长度为10的数组 当添加元素超过10的时候,会进 ...
- ArrayList底层原理
ArrayList底层采用数组实现,访问特别快,它可以根据索引下标快速找到元素.但添加插入删除等写操作效率低,因为涉及到内存数据复制转移. ArrayList对象初始化时,无参数构造器默认容量为10, ...
- JAVA容器-模拟ArrayList的底层实现
概述 ArrayList实质上就是可变数组的实现,着重理解:add.get.set.remove.iterator的实现,我们将关注一下问题. 1.创建ArrayList的时候,默认给数组的长度设置为 ...
- ArrayList底层实现原理
ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括null在内的所有元素.除了实现列表接口外,此类还提供一些方法来操作内部用来存储列表的数组 ...
- ArrayList 底层实现原理
ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...
- JAVA SE ArrayList 底层实现
Array 查询效率高,增删效率低( Link 增删效率高 Vector 线程安全 List 列表 源代码: package com.littlepage.test; /** * 基于底层实现Arra ...
- Java——ArrayList底层源码分析
1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数 ...
- ArrayList底层代码解析笔记
通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...
- 模拟ArrayList
package com.helloidea; import java.util.ArrayList; import java.util.Collection; import java.util.Lis ...
随机推荐
- oc17--点语法
// // Person.h // day13 #import <Foundation/Foundation.h> @interface Person : NSObject { // @p ...
- vim下很好的右键复制方法
1)先按shift ,然后鼠标选中即可复制:(shift按下时为非vim环境) 2)好方法: "Enable and disable mouse use noremap <f1> ...
- DNS 隐蔽通道工具资料汇总
http://www.cnblogs.com/bonelee/p/7651746.html DNS隧道和工具 内含dns2tcp.iodine.dnscat2工具的简单使用说明 iodine工具的使用 ...
- 2-1 Restful中HTTP协议介绍
Restful是一种基于资源的软件架构风格,所以从定义上来说是跟HTTP无关的.但是本课程提到的Restful API是基于HTTP协议的一种实现.所有相关知识都是基于现有的HTTP协议而来,并没有对 ...
- Arduino-1602-LiquidCrystal库
前言:LiquidCrystal是一个1602的IIC库,使用IIC协议可以极大节约用线数量,十分方便.当然,前提是1602要使用LCD1602 I2C模块. 一.库函数快速查询 LiquidCrys ...
- 【正则表达式】从json数组中抽取id列表
有如下数组,要从中取出id: "[\"3812662409\",\"3812633637\",\"3812627686\",\&q ...
- 5.Project常用操作介绍
Project常用操作介绍 1.项目浏览器 2.项目属性 Name:项目名称 Category:项目组织结构 Author:作者 Copyright:版权 Image:项目图标 Description ...
- a better git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d ...
- houxiurong.com 关于Tomcat7部署 一台机器部署两个项目,一个用域名访问,一个用IP访问
该内容来自 http://houxiurong.com,转载请说明出处. 1.使用IP访问的项目放在Tomcat7 的webapps目录下面:比如:AAA 2.使用域名访问的项目放在Tomcat7的w ...
- form 表单的另类触发方式:报错触发
在用form表单提交的时候,遇到一个问题:表单未验证完,表单就提前提交了. 然后通过断点调试,发现form提交会因为函数报错提前提交. 即如果你的form提交过程中,没有执行到return true之 ...