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. 数据库是.frm,.myd,myi备份如何导入mysql (转)

    今天找了个案例,琢磨了半天,才分析大概出来,数据库是.frm,.myd,myi备份,不会导入mysql,到网上找了些资料,导入成功. 首先说一下这几种文件是干什么的,*.frm是描述了表的结构,*.M ...

  2. 在Salesforce中用Data Loader去批量处理数据

    Data Loader download file: Setup --> Administration Setup --> Data Loader --> Download the ...

  3. tree view

    <TreeView x:Name="treeParameter" Width=" Margin="11,6,11,6" ItemsSource= ...

  4. 【Web前端】---js调用本地应用程序

    最近进入了一个项目组,向大牛们一起学习如何搞开发,可谓是边开发边学习.就在前两天,我们的项目被领导们验收了一次,顺便给我们提了点新的需求,要求我们能够使用外在设备拍照上传.君要臣死,臣不能不死.更何况 ...

  5. SQL Case when 的使用方法(转)

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  6. C# 使用线程池,设置每个线程的执行时间,过了时间强制结束

    改用thread来驱动这个方法先建一个类下面的成员,来存放进程List<Thread> pool = new List<Thread>();在需要启动你的方法的时候建进城Thr ...

  7. Codeforce 546D

    Soldier and Number Game Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  8. HRESULT:0x80070057 (E_INVALIDARG)的异常

    错误信息: 未能加载文件或程序集……或它的某一个依赖项.参数不正确. (异常来自 HRESULT:0x80070057 (E_INVALIDARG)) English:Could not load f ...

  9. Find Minimum in Rotated Sorted Array

    package leetcode; /* * * 注意问题: * 1. 原序列升序.降序问题,两种情况都要考虑 * 2. 边界问题,如果只有两个元素时要单独考虑,在num[mid]==num[left ...

  10. ZOJ3201 Tree of Tree(树形DP)

    题目大概求一棵树中大小为k的子树的最大权和. dp[u][k]表示以u为根的子树中包含u结点的大小为k的子树的最大权和,然后树上背包转移转移很容易.. #include<cstdio> # ...