Java中ArrayList的自我实现
对于ArrayList相比大家都很熟悉,它是java中最常用的集合之一。下面就给出它的自我实现的java代码。
需要说明的一点是,它是基于数组创建的。所以它在内存中是顺序存储,对于查找十分的方便。
package com.pinjia.shop.common.collection; import java.util.Iterator;
import java.util.NoSuchElementException; /**
* Created by wangwei on 2017/1/3.
* ArrayList的自己封装,实现
*/
public class MyArrayList<E> implements Iterable<E> { private static final int DEFAULT_SIZE = 10;//容器默认大小
private int theSize;
private E[] theItems; public MyArrayList(){
clear();
} public void clear(){
theSize = 0;
ensureCapacity(DEFAULT_SIZE);
} public int size(){
return theSize;
} public boolean isEmpty(){
return size() == 0;
} public void trimToSize(){
ensureCapacity(size());
} public E get(int idx){
if(idx<0||idx>=size())
throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
} public E set(int idx,E newVal){
if(idx<0||idx>size())
throw new ArrayIndexOutOfBoundsException();
E old = theItems[idx];
theItems[idx] = newVal;
return old;
} public void ensureCapacity(int newCapacity) { //数组的扩展
if(newCapacity<theSize)
return;
E[] old = theItems;
theItems = (E[])new Object[newCapacity];
for(int i=0;i<size();i++){
theItems[i] = old[i];
}
} public boolean add(E x){
add(size(),x);
return true;
} public void add(int idx,E x){
if(theItems.length == size())
ensureCapacity(size()*2+1);
for(int i=theSize;i>idx;i--){
theItems[i] = theItems[i-1];
}
theItems[idx] = x;
theSize++;
} public E remove(int idx){
E removeItem = theItems[idx];
for(int i=idx;i<size()-1;i++){
theItems[i] = theItems[i+1];
}
theSize--;
return removeItem;
} public Iterator<E> iterator() {
return new ArrayListIterator();
} private class ArrayListIterator implements Iterator<E>{ //内部类
private int current = 0;
public boolean hasNext(){
return current<size();
}
public E next(){
if(!hasNext())
throw new NoSuchElementException();
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
}
Java中ArrayList的自我实现的更多相关文章
- java中ArrayList 、LinkList区别
转自:http://blog.csdn.net/wuchuanpingstone/article/details/6678653 个人建议:以下这篇文章,是从例子说明的方式,解释ArrayList.L ...
- JAVA中ArrayList用法
JAVA中ArrayList用法 2011-07-20 15:02:03| 分类: 计算机专业 | 标签:java arraylist用法 |举报|字号 订阅 Java学习过程中做题时 ...
- Java中ArrayList与LinkedList的区别
Java中ArrayList与LinkedList的区别 一般大家都知道ArrayList和LinkedList的区别: 1. ArrayList的实现是基于数组,LinkedList的实现是基于双向 ...
- Java中arraylist和linkedlist源代码分析与性能比較
Java中arraylist和linkedlist源代码分析与性能比較 1,简单介绍 在java开发中比較经常使用的数据结构是arraylist和linkedlist,本文主要从源代码角度分析arra ...
- java中ArrayList 和 LinkedList 有什么区别
转: java中ArrayList 和 LinkedList 有什么区别 ArrayList和LinkedList都实现了List接口,有以下的不同点:1.ArrayList是基于索引的数据接口,它的 ...
- Java中ArrayList相关的5道面试题
本文参考了 <关于ArrayList的5道面试题 > 1.ArrayList的大小是如何自动增加的? 这个问题我想曾经debug过并且查看过arraylist源码的人都有印象,它的过程是: ...
- Java中ArrayList类详解
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...
- Java中ArrayList源码分析
一.简介 ArrayList是一个数组队列,相当于动态数组.每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保 ...
- Java中ArrayList和LinkedList差别
一般大家都知道ArrayList和LinkedList的大致差别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机訪问get和set.A ...
随机推荐
- ISO 基础之 (十二) 文件管理
一 文件管理 沙盒:让每个APP应用在手机上有一个独立的文件夹,相互之间不能访问. 沙盒目录:NSHomeDirectory() library: 库文件 tmp: 临时文件 1.NSData 也是一 ...
- 项目总结—jQuery EasyUI-DataGrid动态加载表头
http://blog.csdn.net/zwk626542417/article/details/19248747 概要 在前面两篇文章中,我们已经介绍了在jQuery EasyUI-DataGri ...
- Json序列化对象
之前都是用的二进制的序列化方法,是.net自带的,但是最常用到的还是Json序列化 (1)只需要调用 Newtonsoft.Json.dll 即可 public class JsonTools { / ...
- 初学Hibernate之Query扩展
1.hql参数化查询,不明确值类型的用setParameter方法:明确查询结果为一条记录的用uniqueResult方法查询 注意,参数化查询中方法setString 或 setParameter如 ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- 连接oracle读取数据
没怎么用过oracle,而且是在地税内网内部估计是防火墙的原因虚拟机里也连不上oracle,刚开始费了很多周折查找问题,现在又放弃使用直连数据库了,记下来以备后用吧 public class Load ...
- rsync+inotify 实现服务器之间目录文件实时同步(转)
软件简介: 1.rsync 与传统的 cp. tar 备份方式相比,rsync 具有安全性高.备份迅速.支持增量备份等优点,通过 rsync 可 以解决对实时性要求不高的数据备份需求,例如定期的备份文 ...
- jQuery Uploadify在ASP.NET MVC中的使用
感谢http://www.cnblogs.com/libingql/archive/2012/09/11/2681007.html 除此之外,给大家推荐一个: http://gallery.kissy ...
- c#接口和抽象类对比学习
什么是接口? 接口就是一种规范,协议(*),约定好遵守某种规范就可以写通用的代码. 定义了一组具有各种功能的方法.接口描述的是一种能力,具有这种能力的事物可以没任何关系.比如: public inte ...
- vim显示行号
在Linux环境下的编辑器有vi.vim.gedit等等.进入这些编辑器之后,为了方便我们需要编辑器显示出当前的行号,可偏偏编辑器默认是不会显示行号的.我们有二种办法可以解决: 第一种是,手动显示:在 ...