package other;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException; /*
* ArrayList泛型类的实现
*/
public class MyArrayList<AnyType> implements Iterable<AnyType> { private static final int DEFAULT_CAPACITY = 10; private int theSize;
private AnyType[] theItems; public MyArrayList() {
clear();
} public void clear() {
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
} private int size() {
return theSize;
} public boolean isEmpty() {
return size() == 0;
} public void trimToSize() {
ensureCapacity(size());
} public AnyType get(int index) {
if (index < 0 || index >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
return theItems[index];
} private AnyType set(int index, AnyType newVal) {
if (index < 0 || index >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
AnyType old = theItems[index];
theItems[index] = 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 < size(); i++) {
theItems[i] = old[i];
}
} public void add(int index, AnyType x) {
if (theItems.length == size()) {
ensureCapacity(size() * 2 + 1);
}
for (int i = theSize; i < index; i--) {
theItems[i] = theItems[i - 1];
}
theItems[index] = x; theSize++;
} public boolean add(AnyType x) {
add(size(), x);
return true;
} public AnyType remove(int index) {
AnyType removedItem = theItems[index];
for (int i = index; i < size() - 1; i++) {
theItems[i] = theItems[i + 1];
}
theSize--;
return removedItem;
} public Iterator<AnyType> iterator() { return new ArrayListIterator();
} private class ArrayListIterator implements Iterator<AnyType> { private int current = 0; public boolean hasNext() { return current < size();
} public AnyType next() { if (!hasNext()) {
throw new NoSuchElementException();
}
return theItems[current++];
} public void remove() {
MyArrayList.this.remove(--current);
} } }

ArrayList类的实现的更多相关文章

  1. 集合 ArrayList 类

    集合的基本信息: System.Collections   系统类中的收藏类,定义各种对象(如列表,队列,位数组,哈希表和字典)的集合 常用的集合为ArrayList类:特殊集合一般会用到Queue队 ...

  2. Java API —— ArrayList类 & Vector类 & LinkList类

    1.ArrayList类     1)ArrayList类概述         · 底层数据结构是数组,查询快,增删慢         · 线程不安全,效率高     2)ArrayList案例   ...

  3. C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)

    1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法 ...

  4. Java中ArrayList类详解

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  5. ArrayList 类和List<T>泛型类

    ArrayList集合类在System.Colletions命名空间下,它其实是一个特殊的数组,它可以动态的添加和删除元素,根据元素的改变自动决定它自身的大小,也可以灵活的插入元素等操作,使用起来要比 ...

  6. 表的顺序结构---重写Arraylist类

    重写ArrayList类,为防止冲突,重写为MyArrayList,未继承Iterable类. public class MyArrayList<AnyType>{ int N=10; A ...

  7. JDK1.8源码(五)——java.util.ArrayList 类

    关于 JDK 的集合类的整体介绍可以看这张图,本篇博客我们不系统的介绍整个集合的构造,重点是介绍 ArrayList 类是如何实现的. 1.ArrayList 定义 ArrayList 是一个用数组实 ...

  8. LinkedList类 和ArrayList类

    1)LinkedList类  LinkedList实现了List接口,允许null元素.此外LinkedList提供额外的get,remove,insert方法在 LinkedList的首部或尾部.这 ...

  9. 实现一个自定义的ArrayList类,实现将原List中的每个数据都乘以10

    1.首先自定义一个Operate接口,如下所示: public interface Operate { public Integer caozuo(Integer i); } 2.实现自定义的Arra ...

  10. java基础之集合框架--使用ArrayList类动态 存储数据

    一.ArrayList是List接口下的一个实现类,实现了长度可变的.连续的数组:拥有数组的特性. 遵循了LIst的规则:不唯一的.有序的. 如果没有增加泛型的话,集合中可以添加任何类型的数据. 使用 ...

随机推荐

  1. 遍历PspCidTable表检测隐藏进程

    一.PspCidTable概述 PspCidTable也是一个句柄表,其格式与普通的句柄表是完全一样的,但它与每个进程私有的句柄表有以下不同: 1.PspCidTable中存放的对象是系统中所有的进程 ...

  2. 6.android加密解析

    编码.数字摘要.加密.解密 UrlEncoder /Urldecoder String str = "http://www.baidu.com?serach = 哈哈"; Stri ...

  3. DOM--3 DOM核心和DOM2 HTML(3)

    核心Element对象 操作Element对象的属性 为了简化对attributes的处理,Element对象中包含了很多用来操纵Node对象的attributes属性的方法: getAttribut ...

  4. iOS10 UI教程视图调试

    iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...

  5. WPF,解决Listbox,按住ListboxItem向下拖出Listbox,横向滚动条跑到最后。

    类似这种样式的控件,.,在横向滚动条隐藏的情况下有这样的问题.(横向滚动条显示的时候也会,,目前不知道怎么解决.) 因为这个控件偏移是利用ListBox的ItemsPanelTemplate模版里的S ...

  6. 指针与const

    指向常量的指针,不能用于改变其所指对象的值. 指针的类型必须与所指对象的类型一致,但是有两个例外,第一种是允许令一个指向常量的指针指向一个非常量对象: double dra1 = 3.14; cons ...

  7. Sharepoint超期触发列表工作流提醒

    项目背景 Sharepoint 2010 ,Infopath 2010环境,用Infopath设置好表单把数据提交到Sharepoint的Library库.很常见的需求,其中有一个[状态]字段,[申请 ...

  8. HDU4609 3-idiots(母函数 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...

  9. SQL SERVER 和ACCESS的数据导入导出

            //批量导入Access         string filepath = Server.MapPath("student.mdb");         stri ...

  10. WPF学习笔记(3):Path绘制命令zz

    WPF的XAML提供了一系列功能强大.用法复杂的 mini-language 来描述可扩展应用程序标记语言 (XAML) 中的几何路径.如下所示: XAML <Canvas>   < ...