您的位置:首页 > 编程语言 > Java开发

java的ArrayList的简单实现

2016-04-27 17:08 537 查看
ArrayList的实现的核心方法就是数组,数组在内存中的地址是连续的,所以支持快速访问的。如果你想用一种数据结构,既能快速访问,又能快速在末端添加数据(arraylist在末端添加数据没有数组的数据移动),那么ArrayList是一种理想的数据结构。下面展现简单实现ArrayList的代码,不足之处,请大家告诉我。

package swu.comput;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
*
* @author shen-pc java简单ArrayList的实现
* @param <T>
*/
public class ForLoop<T> implements Iterable<T> {

// 存储每一个值的数组
private T[] list;
// 数组长度
private int size;
private static final int DEFAULT_CAPACITY = 10;

public ForLoop() {
clear();
}

/**
* 用于初始化一个长度为10的数组
*/
public void clear() {
size = 0;
ensureCapacity(DEFAULT_CAPACITY);
}

/**
* 返回表的长度
*
* @return 返回表的长度,-1表示数组为空,没有初始化
*/
public int size() {
if (null == list) {
return -1;
}
return size;
}

/**
* 把数组中没有存储值的位置“截取掉”
*/
public void trimToSize() {
ensureCapacity(size);
}

/**
*
* @param pos
* @return 返回位于表pos位置的值
*/
public T get(int pos) {
if (pos < 0 || pos > size) {
throw new ArrayIndexOutOfBoundsException();
}
return list[pos];
}

/**
*
* @param t
*            需要改变后的值
* @param pos
*            要改变值的位置
* @return 返回被改变的值
*/
public T set(T t, int pos) {
// 判断位置是否有效
if (pos < 0 || pos >= size) {
throw new ArrayIndexOutOfBoundsException();
}
T old = list[pos];
list[pos] = t;
return old;
}

/**
* 扩大数组的长度
*
* @param newCapacity
*            需要扩大表的长度
*/
public void ensureCapacity(int newCapacity) {
if (size > newCapacity) {
return;
}
T[] oldList = list;
list = (T[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
list[i] = oldList[i];
}
// 显示置空,因为oldList一直指向没有扩充长度的值
oldList = null;
}

public void add(T t) {
add(t, size);
}

/**
* 在pos的位置添加值,以前pos处的值向后移
*
* @param t
* @param pos
*/
public void add(T t, int pos) {
if (pos < 0) {
throw new ArrayIndexOutOfBoundsException();
} else if (pos > size) {
// 测试java的arraylist发现,这里会抛出这种异常
throw new IndexOutOfBoundsException("Index: " + pos + ", " + "Size: " + size);
}

if (size == list.length) {
// +1是为了list的size是1的情况出现
ensureCapacity(list.length * 2 + 1);
}
// 把pos位置以及pos位置以后的值向后移动
for (int i = size; i > pos; i--) {
list[i] = list[i - 1];
}
list[pos] = t;
size++;
}

/**
* 移除pos位置的值,并且所有后面的值向前移动
*/
public void remove(int pos) {
if (pos < 0 || pos > size - 1) {
throw new ArrayIndexOutOfBoundsException();
}
for (int i = pos; i < size - 1; i++) {
list[i] = list[i + 1];
}
list[size - 1] = null;
size--;
}

/**
* 内部类,和forloop对象关联在一起
*
* @author shen-pc
*
* @param <T>
*/
private class MyIterator<T> implements Iterator<T> {

private int currunt = 0;

@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if (currunt < size) {
return true;
}
return false;
}

@Override
public T next() {
// TODO Auto-generated method stub
if (hasNext() == true) {
return (T) list[currunt++];
} else {
throw new NoSuchElementException();
}

}

@Override
public void remove() {
// TODO Auto-generated method stub
// 调用外部内定义的remove方法
ForLoop.this.remove(--currunt);
}

}

/**
* 返回一个iterator对象
*/
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new MyIterator<>();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: