思路:

一 载体

ArrayList是一个集合容器,必然要有一个保存数据的载体。

public class MyArraylist {
private final static int INIT_COUNT = 10; Object[] arrays; public MyArraylist() {
this(INIT_COUNT);
} public MyArraylist(int count) {
arrays = new Object[count];
}
}

二属性

长度

得到集合会初始化一个数组长度,集合的元素个数不能是数组长度。

public class MyArraylist2 {
private int size;
public int size(){
return size;
} }

三方法

增删改查

增加

按默认索引加入元素,按索引加入元素,加入单个元素,加入批量元素。这里只举按单个加入元素的例子。

首先需要判断索引是否非法

public void checkIndexRange(int index) {
if (index > size) {
throw new IndexOutOfBoundsException("数组越界");
}
} public void checkIndexRangeForAdd(int index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException("数组越界");
}
}

判断当前数组是否需要扩容。如果索引大于当前数组长度,按当前长度的50%增加。并将当前数组复制到新的数组。

public void checkIndex(int index) {
if (index > arrays.length) {
int oldLength = size;
int newLength = oldLength + (index >> 1);
arrays = Arrays.copyOf(arrays, newLength);
}
}

增加单个元素

    public void add(Object obj, int index) {
checkIndexRangeForAdd(index);//判断索引是否非法
checkIndex(size + 1);//判断数组是否需要扩容
System.arraycopy(arrays, index, arrays, index + 1, size - index);
arrays[index] = obj;
size++;
} public boolean add(Object obj) {
checkIndex(size + 1);//判断数组是否需要扩容
arrays[size++] = obj;
return true;
}

获取元素

public T get(int index) {
checkIndexRangeForAdd(index);
return (T) arrays[index];
}

更新元素

    public T set(Object obj, int index) {
checkIndexRange(index);
T t = (T) arrays[index];
arrays[index] = obj;
return t;
}

删除元素

public T remove(int index) {
checkIndexRange(index); T t = (T) arrays[index];
int moveLength = size - index - 1;
if (moveLength > 0) {
System.arraycopy(arrays, index + 1, arrays, index, moveLength);
}
arrays[--size] = null;
return t;
}

四迭代

使用增强型for循环迭代输出,以及Java 8引进的lambda表达式实现foreach。

首先生成一个迭代器。

private class MyArrayListIterator implements Iterator<T> {

        private int count;

        public boolean hasNext() {
return count != size;
} public T next() {
checkModcount();
return (T) arrays[count++];
} }

然后继承Iterable接口。

public class MyArraylist2<T> implements Iterable<T>{

    @Override
public Iterator<T> iterator() {
return new MyArrayListIterator();
} }

五线程安全问题

ArrayList没有实现线程安全,但是在迭代的时候,不允许修改。

ArrayList使用一个成员变量modCount在迭代开始的时候记录ArrayList正在做修改(增加,修改,删除)的数量,然后在每迭代一行的时候,去比较modCount是否发生变化。如果发生变化,证明此刻有其它的线程或者就是本线程就对它进行修改,然后抛出异常。

定义modcount变量。

private transient int modcount;

在增加,修改,删除的时候,均需将此值加1。

public boolean add(Object obj) {
checkIndex(size + 1);
modcount++;//加1
arrays[size++] = obj;
return true;
} public void checkIndex(int index) {
if (index > arrays.length) {
modcount++;//加1
int oldLength = size;
int newLength = oldLength + (index >> 1);
arrays = Arrays.copyOf(arrays, newLength);
}
}
public T set(Object obj, int index) {
checkIndexRange(index);
modcount++;//加1
T t = (T) arrays[index];
arrays[index] = obj;
return t;
}
public T remove(int index) {
checkIndexRange(index);
modcount++;//加1 @SuppressWarnings("unchecked")
T t = (T) arrays[index];
int moveLength = size - index - 1;
if (moveLength > 0) {
System.arraycopy(arrays, index + 1, arrays, index, moveLength);
}
arrays[--size] = null;
return t;
}

在迭代的时候,将此值赋给另一个变量exceptCount。然后再每迭代一行的时候,将exceptCount与modcount比较。

private class MyArrayListIterator implements Iterator<T> {

        private int exceptCount = modcount; 

        public void checkModcount() {
if (exceptCount != modcount) {
throw new ConcurrentModificationException();
}
} }

最后实现的全部代码

package com.skycomm.sdf;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator; public class MyArraylist<T> implements Iterable<T> { private final static int INIT_COUNT = 10; private int size = 0; private transient int modcount; Object[] arrays; public MyArraylist() {
this(INIT_COUNT);
} public MyArraylist(int count) {
arrays = new Object[count];
} public void add(Object obj, int index) {
checkIndexRangeForAdd(index);
checkIndex(size + 1);
System.arraycopy(arrays, index, arrays, index + 1, size - index);
arrays[index] = obj;
size++;
} public boolean add(Object obj) {
checkIndex(size + 1);
modcount++;//加1
arrays[size++] = obj;
return true;
} public void checkIndex(int index) {
if (index > arrays.length) {
modcount++;//加1
int oldLength = size;
int newLength = oldLength + (index >> 1);
arrays = Arrays.copyOf(arrays, newLength);
}
} public T get(int index) {
checkIndexRangeForAdd(index);
return (T) arrays[index];
} public T remove(int index) {
checkIndexRange(index);
modcount++;//加1 @SuppressWarnings("unchecked")
T t = (T) arrays[index];
int moveLength = size - index - 1;
if (moveLength > 0) {
System.arraycopy(arrays, index + 1, arrays, index, moveLength);
}
arrays[--size] = null;
return t;
} public T set(Object obj, int index) {
checkIndexRange(index);
modcount++;//加1
T t = (T) arrays[index];
arrays[index] = obj;
return t;
} public void checkIndexRange(int index) {
if (index > size) {
throw new IndexOutOfBoundsException("数组越界");
}
} public void checkIndexRangeForAdd(int index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException("数组越界");
}
} public int size() {
return size;
} public boolean isEmpty() {
return size == 0;
} public String toString() {
Iterator<T> it = iterator();
if (!it.hasNext())
return "[]"; StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
T e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (!it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
} public Iterator<T> iterator() {
return new MyArrayListIterator();
} private class MyArrayListIterator implements Iterator<T> { private int count; private int exceptCount = modcount; public boolean hasNext() {
return count != size;
} public T next() {
checkModcount();
return (T) arrays[count++];
} public void checkModcount() {
if (exceptCount != modcount) {
throw new ConcurrentModificationException();
}
} } }

自己实现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. rt-thread平台 动态装载实现原理

    原理分析: a.在链接脚本link.lds里定义了专门存放动态符号表的段RTMSymTab. b.内核对外提供可延时绑定的接口在rtm.h中通过RTM_EXPORT将一对对符号+地址构成的表存放到RT ...

  2. JavaBean找到项目下的文件这里是\WEB-INF\classes\下面的属性文件

    String baseinfo="/com/Bean/DBhelp.properties"; filename=getClass().getClassLoader().getRes ...

  3. listener.starting()源码探究

    容器启动时,监听器调用starting方法,本质是找到匹配的监听器,广播事件,开始监听 上节对寻找匹配监听器做了分析,其是这个过程比较重要的部分,本文开始监听,反而不是那么复杂,如下: 找匹配监听器就 ...

  4. C++ 64位操作系统调用 RegOpenKey() 读取注册表,返回 2, ERROR_FILE_NOT_FOUND

    环境:64位操作系统, VS2017 首先在命令行执行 REG ADD HKLM\Software\seastarsun /v serial /t REG_SZ /d 58ae4cb077a4e1 在 ...

  5. TNS-12541: TNS: 无监听程序 解决方案

    转自   感谢   https://www.cnblogs.com/yx007/p/6732012.html 问题描述 在用PL/SQL Developer连接Oracle 11g时报错“ORA-12 ...

  6. 部署Redis4.x单机版及配置RDB和AOF持久化

    一.环境及软件 OS soft version  CentOS 7.5   redis-4.0.12(目前是4.x最新) 二.下载及编译Redis [root@localhost ~]# wget h ...

  7. mysql 多行(GROUP_CONCAT)和多列(CONCAT)的合并函数

    1,多行合并:把查询的一行或者多行进行合并. SELECT GROUP_CONCAT(md.data1) FROM DATA md,contacts cc WHERE md.conskey=cc.id ...

  8. Android 开发 PopupWindow弹窗

    简介 PopupWindow,顾名思义弹窗.PopupWindow是与AlertDialog在形式上类似的弹窗功能,都是为了在activity最上层显示一个弹窗.但是区别是PopupWindow可以自 ...

  9. Maven 错误 :The POM for com.xxx:jar:0.0.1-SNAPSHOT is invalid, transitive dependencies (if any) will not be available

    一个大的maven 项目,结构是一个根pom,下面几个小的module,包括了appservice-darc,appservice-entity等,其中appservice-darc 依赖了 apps ...

  10. gp工具的许可

    还是要在代码里许可 static class Program { [STAThread] static void Main() { ESRI.ArcGIS.RuntimeManager.Bind(ES ...