最近在学习数据结构和算法,书上有个ArrayList的简单实现,写的很不错。

 package cn.sp.test4;

 import java.util.Iterator;
import java.util.NoSuchElementException; /**
* Created by 2YSP on 2017/10/9.
*/
public class MyArrayList<AnyType> implements Iterable<AnyType> { private static final int DEFAULT_CAPACITY = 10; private int theSize;
private AnyType[] theItems; public MyArrayList(){
doClear();
} private void doClear() {
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
} public int size(){
return theSize;
} public boolean isEmpty(){
return size() == 0;
} public void trimToSize(){
ensureCapacity(size());
} /**
* 获取角标处的值
* @param idx
* @return
*/
public AnyType get(int idx){
if (idx < 0 || idx >= size()){
//角标越界异常
throw new ArrayIndexOutOfBoundsException();
} return theItems[idx];
} /**
* 替换目标位置的值,并返回旧值
* @param idx
* @param newVal
* @return
*/
public AnyType set(int idx , AnyType newVal){
if (idx < 0 || idx >= size()){
//角标越界异常
throw new ArrayIndexOutOfBoundsException();
} AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
} public void ensureCapacity(int newCapacity) {
if (newCapacity < theSize){
return;
} AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapacity];
for(int i = 0 ;i < old.length ; i++){
theItems[i] = old[i];
}
} public boolean add(AnyType x){
add(size(),x);
return true;
} public void add(int idx,AnyType 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 AnyType remove(int idx){
AnyType removedItem = theItems[idx];
for(int i = idx ; i <size()-1;i++){//依次前移
theItems[i] = theItems[i+1];
}
theSize--;
return removedItem;
} @Override
public Iterator<AnyType> iterator() {
return new ArrayListIterator();
} private class ArrayListIterator<AnyType> implements java.util.Iterator<AnyType>{ private int current = 0; @Override
public boolean hasNext() {
return current < size();
} @Override
public AnyType next() {
if (!hasNext()){
throw new NoSuchElementException();
} return (AnyType) theItems[ current++ ];
} @Override
public void remove() {
MyArrayList.this.remove(current--);
}
}
}

注意理解current++和current--

实现自己的ArrayList的更多相关文章

  1. 计算机程序的思维逻辑 (38) - 剖析ArrayList

    从本节开始,我们探讨Java中的容器类,所谓容器,顾名思义就是容纳其他数据的,计算机课程中有一门课叫数据结构,可以粗略对应于Java中的容器类,我们不会介绍所有数据结构的内容,但会介绍Java中的主要 ...

  2. 分享个 之前写好的 android 文件流缓存类,专门处理 ArrayList、bean。

    转载麻烦声明出处:http://www.cnblogs.com/linguanh/ 目录: 1,前序 2,作用 3,特点 4,代码 1,前序  在开发过程中,client 和 server 数据交流一 ...

  3. 深入理解java中的ArrayList和LinkedList

    杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...

  4. 【干货】用大白话聊聊JavaSE — ArrayList 深入剖析和Java基础知识详解(二)

    在上一节中,我们简单阐述了Java的一些基础知识,比如多态,接口的实现等. 然后,演示了ArrayList的几个基本方法. ArrayList是一个集合框架,它的底层其实就是一个数组,这一点,官方文档 ...

  5. WebAPI接口返回ArrayList包含Dictionary对象正确解析

    一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...

  6. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

  7. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  8. ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量

    当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...

  9. Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  10. C#语言基础——集合(ArrayList集合)

    集合及特殊集合 集合的基本信息: System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...

随机推荐

  1. C# 读自己的资源文件

    Assembly assm = this.GetType().Assembly;//Assembly.LoadFrom(程序集路径); foreach (string resName in assm. ...

  2. 查看MySQL系统变量的命令

    用了好长时间mysql,却没有用心记住一些有用的东西,加油! mysql> SHOW VARIABLES; +---------------------------------+-------- ...

  3. SD/MMC的Commands和Responses的总结

    SD总线通信是基于指令和数据比特流,起始位開始和停止位结束. SD总线通信有三个元素:1.Command:由host发送到卡设备.使用CMD线发送. 2.Response:从card端发送到host端 ...

  4. linux 下shell脚本执行多个命令的方法

    1.每个命令之间用;隔开说明:各命令的执行给果,不会影响其它命令的执行.换句话说,各个命令都会执行,但不保证每个命令都执行成功. 2.每个命令之间用&&隔开说明:若前面的命令执行成功, ...

  5. 重载OverLoad。隐藏new

    <1> using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...

  6. Linux_C——动态库,静态库

    /usr/lib /lib:标准系统库文件          库是一组预先编译好的函数的集合,这些函数都是按照可重用的原则编写的.它们通常有一组相互关联的函数组成以          执行某项常见的任 ...

  7. HBase在大搜车金融业务中的应用实践

    摘要: 2017云栖大会HBase专场,大搜车高级数据架构师申玉宝带来HBase在大搜车金融业务中的应用实践.本文主要从数据大屏开始谈起,进而分享了GPS风控实践,包括架构.聚集分析等,最后还分享了流 ...

  8. 在做java 的web开发,为什么要使用框架

    现在做项目都会使用框架,现在很常见的框架就是SSH(Struts+SpringMVC+spring+hibernate),SSM(Struts/springMVC+Spring+Hibernate), ...

  9. npm学习笔记

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  10. 常用的机器学习&数据挖掘知识点总结

    Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...