ArrayList类的实现,利用数组存储元素
2015-12-01 12:21
459 查看
避免与类库中的ArrayList混淆,我将我实现的类命名为MyArrayList
基本思想:在MyArrayList内部用一个数组来存储插入元素,元素满则扩充数组容量。
实现细节如下:
1、保存基础数组,数组的容量,以及存储在MyArrayList中的当前项;
2、提供一种可以改变数组容量的机制,每当要插入元素且检查到当前存储元素的数组满时,便创建一个两倍容量的新数组,将老数组的元素复制到新数组,并将插入元素也插入新数组,回收老数组(垃圾回收机制)。
3、提供get和set的实现;
4、提供如size()/isEmpty()/clear()/remove/add()等方法;
5、提供一个实现Iterator的类,这个类存储迭代序列中的下一项的下标,提供next()/hasNext()/remove()等方法的实现。
实现代码如下:
基本思想:在MyArrayList内部用一个数组来存储插入元素,元素满则扩充数组容量。
实现细节如下:
1、保存基础数组,数组的容量,以及存储在MyArrayList中的当前项;
2、提供一种可以改变数组容量的机制,每当要插入元素且检查到当前存储元素的数组满时,便创建一个两倍容量的新数组,将老数组的元素复制到新数组,并将插入元素也插入新数组,回收老数组(垃圾回收机制)。
3、提供get和set的实现;
4、提供如size()/isEmpty()/clear()/remove/add()等方法;
5、提供一个实现Iterator的类,这个类存储迭代序列中的下一项的下标,提供next()/hasNext()/remove()等方法的实现。
实现代码如下:
import java.util.Iterator; import java.util.NoSuchElementException; /** * @description ArrayList的实现 * @author Zhangx * @date 2015年12月1日 下午12:34:39 * @version 1.0 */ public class MyArrayList<AnyType> implements Iterable<AnyType>{ private static final int DEFAULT_CAPACITY = 10; //默认初始大小 private int theSize; //当前元素个数 private AnyType[] theItems; //MyArrayList内部用来存储元素的数组 public MyArrayList(){ //调用clear(),初始化List clear(); } /** * 将元素个数设置为0,并初始化一个大小为10的数组存储元素 */ public void clear(){ theSize = 0; ensureCapacity(DEFAULT_CAPACITY); } /** * 获得list元素个数 * @return int */ public int size(){ return theSize; } /** * list是否为空 * @return boolean(true|false) */ public boolean isEmpty(){ return size()==0; } /** * 将list元素放入一个大小为元素个数的新数组 */ public void trimToSize(){ ensureCapacity(size()); } /** * 获得指定位置元素,如果索引超出数组范围则抛出异常 * @param idx * @return AnyType */ public AnyType get(int idx){ if(idx<0||idx>=size()) throw new ArrayIndexOutOfBoundsException(); return theItems[idx]; } /** * 在指定位置设置元素,如果索引超出数组范围则抛出异常 * @param idx * @param newVal * @return AnyType */ public AnyType set(int idx, AnyType newVal){ if(idx<0||idx>=size()){ throw new ArrayIndexOutOfBoundsException(); AnyType old = theItems[idx]; theItems[idx] = newVal; return old; } } /** * 根据传入的大小,获得一个新数组,并将元素存储新数组 * @param newCapacity */ 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]; } } /** * 添加元素 * @param x * @return boolean */ public boolean add(AnyType x){ add(size(),x); return true; } /** * 在指定位置增加元素,如果当前数组已满,则创建一个两倍大小的新数组,然后复制元素,插入元素 * @param idx * @param x */ public void add(int idx, AnyType x){ if(theItems.length==size()){ ensureCapacity(size()*2); } for(int i=theSize;i>idx;i--){ theItems[i] = theItems[i-1]; } theItems[idx] = x; theSize++; } /** * 删除指定位置元素,并将删除元素以后的元素前移一个位置 * @param idx * @return */ 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(); } /** * 迭代器 * @author Zhangxin * */ private class ArrayListIterator implements Iterator<AnyType>{ private int current = 0; //记录一个位置的索引 /** * 判断索引当前位置的下一个位置是否还有元素 */ @Override public boolean hasNext() { return current<size(); } /** * 返回索引当前位置的元素,并指向下一个元素 */ @Override public AnyType next() { if(!hasNext()){ throw new NoSuchElementException(); } return theItems[current++]; } /** * 删除索引前一个位置的元素 */ @Override public void remove() { MyArrayList.this.remove(--current); } } }
相关文章推荐
- Maven资料整理
- 四则运算第二次冲刺更新进度
- 【转】Xcode升到6.4插件失效,与添加插件不小心点击Skip Bundle解决办法
- 四则运算--第二次冲刺
- 盖住关系的验证
- mysql关系(二维表)的设计规范,范式
- Spring Data JPA调用存储过程实例
- Spring Data JPA调用存储过程实例
- 删除了Android Private Libraries之后怎么重新添加进去?
- 仿超级课程表demo
- 基于ajax的简单搜索实现方法
- LintCode: Longest Common Substring
- PS自动生成不同尺寸图标
- 关于android5.1 strings product值引起的复写字串不起效问题的解决方法
- POJ 1664 放苹果
- ZOJ - 2112 Dynamic Rankings(BIT套主席树)
- PhpStorm WebMatrix xDebug 配置开发环境
- 权限之路径拦截
- hough变换是如何检测出直线和圆的?
- FastReport经验