模拟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 ...
随机推荐
- 1016. Phone Bills (25)——PAT (Advanced Level) Practise
题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...
- bzoj2878 [Noi2012]迷失游乐园 [树形dp]
Description 放假了,小Z认为呆在家里特别无聊.于是决定一个人去游乐园玩. 进入游乐园后.小Z看了看游乐园的地图,发现能够将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环 ...
- pip3使用
安装好python3.5,在D:\study\python\python35\Scripts目录下打开命令行 执行命令pip3 list 查看已经安装的东西,提示需要升级,执行命令python -m ...
- Android与JS互相调用以及注意
近期项目中常常使用Html5而Android与JS调用常常会用到,这里记录一下,測试系统5.0以上. 这里先贴一下源代码 Activity: package jwzhangjie.com.webvie ...
- Adding a view
在添加View之前,之前的页面是下面这个样子,需要注意的是浏览器标题,以及浏览器的内容 https://docs.asp.net/en/latest/tutorials/first-mvc-app/a ...
- Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式
ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...
- javascript必须知道的知识要点(一)
该文章不详细叙述各知识要点的具体内容,仅把要点列出来,供大家学习的时候参照,或者检测自己是否熟练掌握了javascript,清楚各个部分的内容. 语句 注释 输出 字面量 变量 数据类型 typeof ...
- Gym-101915C Shahhoud Training Hussain 模拟
题面 题意:每天有K本书,你最多看P本一天,问N天后多少本书没有看 题解:ans=(K-P)*N; 注意一点就是P>=K的时候,ans=0; #include<bits/stdc++.h& ...
- python 3.7 replace函数的坑
使用replace时必须用 str=str.replace(old,new) 如果用 str.replace(old,new)会不起作用. 注意:若str中没有old变量,也不会报错 应用: 练习题 ...
- MySQL实现表之间的字段更新
新功能写好之后,需要把以前表数据更新一下,字段数据从以前的表中获取,只更新两个字段 UPDATE TABLE1,TABLE2 SET TABLE1.COLUMN1 = TABLE2.COLUMN1 , ...