眼过千遍,不如手过一遍。亲身下河知深浅,亲口尝梨知酸甜。
package suanfa;
/**
* 当年学习数据结构和算法的时候,自己写的一个ArrayList
* 可以看到,ArrayList是以数组为基础建立起来的,在容量不够时,需要动态扩展它的容量。
*
* 这里推荐一本学习数据结构和算法的书籍,《数据结构java版》清华大学出版社,译者梁志敏
*
* @author 台哥编程课堂
* https://blog.csdn.net/chaohi
*
* @param <T>
*/
public class ArrayList<T> {
private T[] listArr;
private int listSize;
// 构造方法
public ArrayList() {
listArr = (T[]) new Object[10];
listSize = 0;
}
/**
* 设置更大的容量
*
* @param minCapacity
*/
public void ensureCapacity(int minCapacity) {
// get the current capacity
int currentCapacity = listArr.length;
// only take action if the requested capacity is larger than the
// existing capacity
if (minCapacity > currentCapacity) {
T[] t = (T[]) new Object[minCapacity];
for (int i = 0; i <= listSize - 1; i++) {
t[i] = listArr[i];
}
listArr = t;
}
}
/**
* 把列表容量调整为它的当前大小
*/
public void trimToSize() {
// get the current capacity
int currentCapacity = listArr.length;
// only take action if the current capacity is larger than listSize
if (currentCapacity > listSize) {
T[] t = (T[]) new Object[listSize];
for (int i = 0; i <= listSize - 1; i++) {
t[i] = listArr[i];
}
listArr = t;
}
}
/**
* 验证index是否位于正确的范围内
*/
private void rangecheck(int index, String msg, int upperBound) {
if (index < 0 || index > upperBound) {
throw new IndexOutOfBoundsException("/n" + msg + ": index " + index
+ " out of bounds.Should be in the range 0 to "
+ upperBound);
}
}
/**
* 在索引index处添加元素
*/
public void add(int index, T element) {
// index==listSize is valid; append to the list
this.rangecheck(index, "ArrayList add()", listSize);
// see if we need to reallocate more memory
if (listSize == listArr.length) {
this.ensureCapacity(2 * listArr.length);
}
// shift elements at positions index through listSize-1 to the right
for (int i = listSize - 1; i >= index; i--) {
listArr[i + 1] = listArr[i];
}
// insert element at location index and increment the list size
listArr[index] = element;
listSize++;
}
/**
* 在尾部添加元素并返回true
*/
public boolean add(T element) {
this.add(listSize, element);
return true;
}
/**
* 返回第一个element元素的索引
*/
public int indexOf(T element) {
for (int i = 0; i <= listSize - 1; i++) {
if (listArr[i].equals(element))
return i;
}
return -1;
}
/**
* 返回最后一个element元素的索引
*/
public int lastIndexOf(T element) {
for (int i = listSize - 1; i >= 0; i--) {
if (listArr[i].equals(element))
return i;
}
return -1;
}
/**
* 删除索引index处的元素
*/
public T remove(int index) {
// verify that index is in the proper range
this.rangecheck(index, "ArrayList remove()", listSize - 1);
// save the return value
T returnElement = listArr[index];
// shift elements at indices index+1 to listSize-1 left one position
for (int i = index; i < listSize - 1; i++) {
listArr[i] = listArr[i + 1];
}
// make former last entry a null reference and decrement list size
listArr[listSize - 1] = null;
listSize--;
// return the value that was removed
return returnElement;
}
/**
* 删除列表中的element元素
*/
public boolean remove(T element) {
int index = this.indexOf(element);
if (index != -1) {
this.remove(index);
return true;
}
return false;
}
/**
* 得到索引index处的元素
*/
public T get(int index) {
// verify that index is in the proper range
this.rangecheck(index, "ArrayList get()", listSize - 1);
return listArr[index];
}
/**
* 为索引index处的元素赋值
*/
public T set(int index, T element) {
// verify that index is in the proper range
this.rangecheck(index, "ArrayList set()", listSize - 1);
// save the element at listArr[index]
T previousValue = listArr[index];
// assign the element at index index
listArr[index] = element;
// return the previous element
return previousValue;
}
/**
* 返回列表的长度
*/
public int size() {
return listSize;
}
/**
* 判断列表是否为空
*/
public boolean isEmpty() {
return listSize == 0;
}
/**
* 清空列表
*/
public void clear() {
for (int i = 0; i < listSize; i++) {
listArr[i] = null;
}
listSize = 0;
}
/**
* 判断列表中是否存在元素element
*/
public boolean contains(T element) {
return !(this.indexOf(element) == -1);
}
/**
* 返回对应的数组
*/
public T[] toArray() {
T[] t = (T[]) new Object[listSize];
for (int i = 0; i < listSize; i++) {
t[i] = listArr[i];
}
return t;
}
}