ArrayList类的实现
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类的实现的更多相关文章
- 集合 ArrayList 类
集合的基本信息: System.Collections 系统类中的收藏类,定义各种对象(如列表,队列,位数组,哈希表和字典)的集合 常用的集合为ArrayList类:特殊集合一般会用到Queue队 ...
- Java API —— ArrayList类 & Vector类 & LinkList类
1.ArrayList类 1)ArrayList类概述 · 底层数据结构是数组,查询快,增删慢 · 线程不安全,效率高 2)ArrayList案例 ...
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
1.ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在ArrayList中主要使用Add.Remove.RemoveAt.Insert四个方法对栈进行操作.Add方法 ...
- Java中ArrayList类详解
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...
- ArrayList 类和List<T>泛型类
ArrayList集合类在System.Colletions命名空间下,它其实是一个特殊的数组,它可以动态的添加和删除元素,根据元素的改变自动决定它自身的大小,也可以灵活的插入元素等操作,使用起来要比 ...
- 表的顺序结构---重写Arraylist类
重写ArrayList类,为防止冲突,重写为MyArrayList,未继承Iterable类. public class MyArrayList<AnyType>{ int N=10; A ...
- JDK1.8源码(五)——java.util.ArrayList 类
关于 JDK 的集合类的整体介绍可以看这张图,本篇博客我们不系统的介绍整个集合的构造,重点是介绍 ArrayList 类是如何实现的. 1.ArrayList 定义 ArrayList 是一个用数组实 ...
- LinkedList类 和ArrayList类
1)LinkedList类 LinkedList实现了List接口,允许null元素.此外LinkedList提供额外的get,remove,insert方法在 LinkedList的首部或尾部.这 ...
- 实现一个自定义的ArrayList类,实现将原List中的每个数据都乘以10
1.首先自定义一个Operate接口,如下所示: public interface Operate { public Integer caozuo(Integer i); } 2.实现自定义的Arra ...
- java基础之集合框架--使用ArrayList类动态 存储数据
一.ArrayList是List接口下的一个实现类,实现了长度可变的.连续的数组:拥有数组的特性. 遵循了LIst的规则:不唯一的.有序的. 如果没有增加泛型的话,集合中可以添加任何类型的数据. 使用 ...
随机推荐
- JavaScript中call,apply和prototype
[TOC] call()方法 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 如果没有提供 thi ...
- 在Eclipse中用图形界面的方式获取Salesforce中Object的Query语句
对Salesforce中的Object进行相应的Query是必不可少的操作,大家可以去这个链接去看看官网的解读 http://docs.database.com/dbcom/en-us/db_sos ...
- loj 1377 (bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...
- 通讯录(ios自带有界面)
1.添加AddressBookUI.framework框架 2控制器中实现 #import "ViewController.h" #import <AddressBookUI ...
- Arduino101学习笔记(十三)—— 101六轴传感器
一.相关API 1.begin:需要在其他CUIREIMU前调用 //***************************************************************** ...
- loadrunner生成随机身份证和银行卡号
生成银行卡号码: Action() { char card[19] = {'6','2','2','7','0','0','0','0','0','0','0','0','0','0','0','0' ...
- DataTables - 问题集
1.增加额外搜索条件 var reqData = {}; var extraSearch = []; var oTable = $('table selector').dataTable({ 'aja ...
- Android学习系列(42)--Android Studio实战技巧
使用android studio开发项目的一些问题,功能和技巧. 1. 环境 Mac OSX 10.9.5 + Android Studio 0.8.9 2. gradle项目加载超慢 这是因为gra ...
- happypack 原理解析
说起 happypack 可能很多同学还比较陌生,其实 happypack 是 webpack 的一个插件,目的是通过多进程模型,来加速代码构建,目前我们的线上服务器已经上线这个插件功能,并做了一定适 ...
- 每天一个linux命令--awk
统计计算日志 pmail@app2linux04 performance]$ grep 'user:logBehaviorAction' performance.log|awk -F '|' '{pr ...