import java.util.Iterator;
import java.util.NoSuchElementException; public class MyArrayList<T> implements Iterable<T> {
//默认数组大小
private static final int DEFAULT_CAPACITY=;
//表大小
private int theSize;
//数组存储
private T[] theItems;
//初始化表
public MyArrayList(){
doClear();
}
private void doClear() {
// TODO Auto-generated method stub
theSize=;
ensureCapacity(DEFAULT_CAPACITY);
}
//清空表
public void Clear(){
doClear();
}
//判断是否为空
public boolean isEmpty(){
return size()==;
}
//将此 ArrayList 实例的容量调整为列表的当前大小
public void trimToSize(){
ensureCapacity(size());
} public T get(int index){
if(index<||index>size()){
throw new ArrayIndexOutOfBoundsException();
}
return theItems[index];
}
public T set(int index,T val){
if(index<||index>size()){
throw new ArrayIndexOutOfBoundsException();
}
T old=theItems[index];
theItems[index]=val;
return old;
}
public boolean add(T val){
add(size(),val);
return true;
}
public void add(int index, T val) {
// TODO Auto-generated method stub
if(theItems.length==size()){
ensureCapacity(size()*+);
}
for(int i=theSize;i>index;i--){
theItems[i]=theItems[i-];
}
theItems[index]=val;
theSize++;
}
public T remove(int index){
T removedItem=theItems[index];
for(int i=index;i<size()-;i++){
theItems[i]=theItems[i+];
}
theSize--;
return removedItem;
}
public java.util.Iterator<T> iterator(){
return new ArrayListIterator();
}
private class ArrayListIterator implements java.util.Iterator<T>{
private int current=;
public boolean hasNext(){
return current<size();
}
public T next(){
if(!hasNext()){
throw new NoSuchElementException();
}
return theItems[current++];
}
public void remove(){
MyArrayList.this.remove(--current);
}
}
private void ensureCapacity(int newCapacity) {
// TODO Auto-generated method stub
if(newCapacity<theSize){
return;
}
T[] old=theItems;
theItems=(T[]) new Object[newCapacity];
for(int i=;i<size();i++){
theItems[i]=old[i];
}
}
private int size() {
// TODO Auto-generated method stub
return theSize;
} }

ArrayList数据结构的实现的更多相关文章

  1. 【集合框架】JDK1.8源码分析之ArrayList(六)

    一.前言 分析了Map中主要的类之后,下面我们来分析Collection下面几种常见的类,如ArrayList.LinkedList.HashSet.TreeSet等.下面通过JDK源码来一起分析Ar ...

  2. Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例

    概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...

  3. Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. ArrayList源代码深入剖析

    第1部分 ArrayList介绍ArrayList底层采用数组实现,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, Cloneable, java. ...

  5. 【转】Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例

    原文网址:http://www.cnblogs.com/skywang12345/p/3308556.html 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具 ...

  6. Java 集合系列03之 ArrayList详细介绍

    ArrayList做为List接口中最常用的实现类,必须掌握. 一.ArrayList简介 与Java中的数组相比ArrayList的容量可以动态增加.它继承与AbstractList,实现了List ...

  7. 精通ArrayList,关于ArrayList你想知道的一切

    目录 精通ArrayList,关于ArrayList你想知道的一切 前言 ArrayList 内部结构,和常用方法实现 实例化方法 添加元素 add()方法 get()方法 移除元素 怎么扩容的 序列 ...

  8. java之ArrayList详细介绍

    1  ArrayList介绍 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List ...

  9. ArrayList源码解读(jdk1.8)

    概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...

随机推荐

  1. js中匿名函数和回调函数

    匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...

  2. Knockout示例:User数据CRUD

    模拟数据user.json. { "page": 0, "rows": 0, "total": 161, "isSuccess&q ...

  3. Hadoop ConnectTimeoutException

    晚上继续学习tfidf示例.在跑TwoJob的时候报如下错误,开始以为是node02的防火墙没关好,但看了一下防火墙确实是关了的. 2019-03-30 23:48:19,705 INFO retry ...

  4. zookeeper 学习 zookeeper下载部署

    下载 http://mirror.bit.edu.cn/apache/zookeeper/ 校验 解压后得到zookeeper-3.4.10.jar,使用md5sum zookeeper-3.4.10 ...

  5. 支付宝 app支付 沙盘使用

    文档说明 沙箱测试: App支付沙箱接入注意点 1.app支付支持沙箱接入:在沙箱调通接口后,必须在线上进行测试与验收,所有返回码及业务逻辑以线上为准:2.app支付只支持余额支付,不支持银行卡.余额 ...

  6. 【HDU】HDU5664 Lady CA and the graph

    原题链接 题解 距离省选只有一周了我居然才开始数据结构康复计划 这题很简单,就是点分树,然后二分一个值,我们计算有多少条路径大于这个值 对于一个点分树上的重心,我们可以通过双指针的方法求出它子树里的路 ...

  7. nginx配置2

    第四节 nginx 配置文件 1 keepalive_timeout   65;  设定保存长久连接时长 0代表禁止, 若不设置默认是75s 2keepalive_requests   nu;  在一 ...

  8. JMeter学习笔记2-图形界面简单介绍

    废话少说直接干活的给: 一.打开和运行JMeter,出现UI界面.如图下所示: 工具栏:常见操作的图标集合,有New(新建), Template(模板) ,Save(保存),Start(开始) ,St ...

  9. Period kmp

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  10. Spring(四)使用注解注入Bean

    注解简单介绍 是代码里面的特殊标记,使用注解完成功能. 注解写法@ 注解名称(属性名=属性值). 注解可以作用在类.方法.属性上面. 使用流程: 在ApplicationContext.xml中开启注 ...