您的位置:首页 > 其它

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()等方法的实现。

实现代码如下:

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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: