最近在学习数据结构和算法,书上有个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. [Bzoj1297][Scoi2009 ]迷路 (矩阵乘法 + 拆点)

    1297: [SCOI2009]迷路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1385  Solved: 993[Submit][Status] ...

  2. jquery 关于ajax 及其son

    <%@ page language="java" pageEncoding="UTF-8"%><%@include file="/c ...

  3. 210 Course ScheduleII

    /* * 210 Course ScheduleII * 2016-6-9 by Mingyang * http://www.jyuan92.com/blog/leetcode-course-sche ...

  4. json数组原始字符串

    var a = '{"name":"1234"}';var c = '{["name":"张三","age&q ...

  5. iOS macOS的后渗透利用工具:EggShell

    EggShell是一款基于Python编写的iOS和macOS的后渗透利用工具.它有点类似于metasploit,我们可以用它来创建payload建立侦听.此外,在反弹回的session会话也为我们提 ...

  6. Md5扩展攻击的原理和应用

    *本文原创作者:Guilty and Innocent,本文属FreeBuf原创奖励计划,未经许可禁止转载 做CTF题目的过程中遇到了md5扩展攻击,参考了几篇文章,感觉写的都有些小缺陷,再发一篇文章 ...

  7. 【OpenGL】Shader实例分析(七)- 雪花飘落效果

    转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如 ...

  8. POJ 1988 Cube Stacking(并查集+路径压缩)

    题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...

  9. Zookeeper 简单操作

    1.  连接到zookeeper服务 [java2000_wl@localhost zookeeper-3]$ bin/zkCli.sh -server 127.0.0.1:2181 也可以连接远端的 ...

  10. 移动APP怎样保存用户password

    <span style="font-size:14px;">为了更好的用户体验,移动APPclient一般都会将用户信息进行保存以便兴许能够自己主动登录.</sp ...