顺序表是线性表顺序存储的实现。以下是本人实现的一个的顺序表(写的时候参考了下ArrayList的源码),功能比较简单。大家觉得有可以改进的地方可以提出来哈。
/**
* 自己实现的顺序表
*
*/
public class ArrayList<T> {
// 顺序表的最大长度
private static final int MAX_SIZE = Integer.MAX_VALUE - 1;
// 顺序表的长度
private int size = 0;
// 顺序表的当前位置
private int currentIndex = -1;
// 用来存放数据的数组
private Object[] elementData = null;;
/**
* 默认构造方法,初始化表大小为10
*/
public ArrayList() {
this(10);
}
/**
* 有参构造方法,
*
* @param initCapacity
* 容量大小
*/
public ArrayList(int initCapacity) {
if (initCapacity < 0)
throw new ExceptionInInitializerError();
if (initCapacity > MAX_SIZE)
throw new ArrayIndexOutOfBoundsException();
this.elementData = new Object[initCapacity];
this.size = initCapacity;
}
/**
* 确保是否超过容量,若超过每次10的增量来
*
* @param requireCapacity
* 所需容量
*/
private void ensureCapacity(int requireCapacity) {
if (requireCapacity < 0)
throw new ExceptionInInitializerError();
if (requireCapacity >= this.size) {
int newCapacity = requireCapacity + 10;
if (newCapacity > MAX_SIZE)
newCapacity = MAX_SIZE;
this.size = newCapacity;
this.elementData = Arrays.copyOf(elementData, newCapacity);
}
}
private void checkRange(int index) {
if (index < 0 || index > this.currentIndex)
throw new ArrayIndexOutOfBoundsException();
}
/**
* 增加元素(尾部添加)
*
* @param e
* 要增加的元素
* @return
*/
public boolean add(T e) {
ensureCapacity(currentIndex + 1);
this.elementData[currentIndex + 1] = e;
currentIndex++;
return true;
}
/**
* 按位置插入元素
*
* @param e
* 要插入的元素
* @param index
* 插入的位置
* @return
*/
public boolean insert(T e, int index) {
checkRange(index);
ensureCapacity(currentIndex + 1);
// index以后的元素全部往后面移位
for (int i = this.currentIndex; i >= index; i--) {
this.elementData[i + 1] = this.elementData[i];
}
this.elementData[index] = e;
this.currentIndex++;
return true;
}
/**
* 按位置替换元素
*
* @param e
* 要替换的元素
* @param index
* 位置
* @return
*/
public boolean replace(T e, int index) {
checkRange(index);
this.elementData[index] = e;
return true;
}
/**
* 按引用替换元素
*
* @param e1
* 要被替换的元素
* @param e2
* 新元素
* @return
*/
public boolean replace(T e1, T e2) {
int[] indexs = indexOf(e1);
for (int i = 0; i < indexs.length; i++) {
replace(e2, indexs[i]);
}
return true;
}
/**
* 按位置删除元素
*
* @param index
* 要删除元素的位置
* @return
*/
public boolean remove(int index) {
checkRange(index);
for (int i = index; i != this.currentIndex; i++) {
this.elementData[i] = this.elementData[i + 1];
}
this.elementData[currentIndex] = null;
this.currentIndex--;
return true;
}
/**
* 通过对象引用删除元素
*
* @param e
* 要删除元素的引用
* @return
*/
public boolean remove(T e) {
int[] indexs = indexOf(e);
for (int i = 0; i < indexs.length; i++) {
remove(indexs[i]);
}
return true;
}
/**
* 通过对象引用删除元素
*
* @param e
* 要删除元素的引用
* @param index
* 第一个
* @return
*/
public boolean remove(T e, int index) {
int[] indexs = indexOf(e);
for (int i = 0; i < indexs.length; i++) {
if (index == i)
remove(indexs[i]);
}
return true;
}
/**
* 返回指定元素的位置(没有返回-1)
*
* @param e
* 查找的元素
* @return 位置数组
*/
public int[] indexOf(T e) {
// 返回下标数组
int[] indexs = new int[currentIndex + 1];
// 记录下标个数
int j = 0;
if (e == null || e == "" || e.equals("") || e.equals(null)) {
return new int[] { -1 };
} else {
for (int i = 0; i < elementData.length; i++) {
if (e.equals(elementData[i])) {
indexs[j] = i;
j++;
}
}
if (j == 0)
return new int[] { -1 };
else {
indexs = Arrays.copyOf(indexs, j);
return indexs;
}
}
}
/**
* 判断元素是否在顺序表中
* @param e 要判断的元素
* @return
*/
public boolean contains(T e){
int[] index = indexOf(e);
if(index[0] != -1)
return true;
else
return false;
}
/**
* 通过下标得到元素
* @param index 下标
* @return
*/
@SuppressWarnings("unchecked")
public T get(int index) {
checkRange(index);
return (T) elementData[index];
}
/**
* 获得顺序表当前长度
*
* @return
*/
public int size() {
return currentIndex + 1;
}
/**
* 判断顺序表是否为空
* @return
*/
public boolean isEmpty(){
if(currentIndex == -1)
return true;
else
return false;
}
@Override
public String toString() {
String str = "[";
for (int i = 0; i <= this.currentIndex; i++) {
if (i != this.currentIndex)
str = str + this.elementData[i] + ",";
else
str = str + this.elementData[i];
}
str = str + "]";
return str;
}
}