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. NPOI操作Excel(二)--创建Excel并设置样式

    由于XSSF中的XSSFWorkbook和HSSF中的HSSFWorkbook拥有的属性.方法等都是一样的,故下面就已一个为例做为展示,他们都继承与一个接口:IWorkbook(命名空间:using  ...

  2. 快速部署网络爬虫框架scrapy

    1. 安装Anaconda,因为Anaconda基本把所有需要依赖的环境都一键帮我们部署好了,不需要再操心其他事了,进官网选择需要下载的版本:https://www.anaconda.com/down ...

  3. Quartz.NET(任务调度)与Topshelf(服务)的综合使用

    http://www.cnblogs.com/jys509/p/4628926.html http://cron.qqe2.com/ Quartz_Topshlf_Demo.7z

  4. vue生命周期探究(一)

    前言 在使用vue开发的过程中,我们经常会接触到生命周期的问题.那么你知道,一个标准的工程项目中,会有多少个生命周期勾子吗?让我们来一起来盘点一下: 根组件实例:8个 (beforeCreate.cr ...

  5. 使用jquery.more.js上滑加载更多

    html: <div id="more"> <div class="single_item"> <div class=" ...

  6. CentOS7中Docker-ce的卸载和安装

    一.查看是否已安装了Docker软件包: #查看是否已经安装的Docker软件包sudo yum list installed | grep docker 二.如果已安装不想要的docker/dock ...

  7. Java基础知识➣多线程编程(五)

    概述 Java 给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径.使用多线程也是为了充分的利用服务器资源, ...

  8. Python_xml

    xml: 可扩展标记语言,用来标记数据,定义数据类型,主要用来传输和存储数据(和json差不多,不同语言或程序之间进行数据交换的协议) ET(xml.etree.ElementTree)中的几个类: ...

  9. 执行shell脚本提示“-bash: ./checkP.sh: /bin/sh^M: bad interpreter: No such file or directory”解决方法

    在windows机器下新建了一个shell脚本如下

  10. JMeter通过自定义jar调用和BeanShell源码

    自定义jar包引用 原始java代码,代码的作用的是根据指定的字符串,生成执行长度的随机字符串 package com; import java.util.Random; public class r ...