您的位置:首页 > 其它

自己写的一个ArrayList

2009-09-22 16:18 246 查看
@SuppressWarnings("unchecked")
public class ArrayList<T>  {

private T[] listArr;
private int listSize;

//构造方法
public ArrayList(){
listArr = (T[]) new Object[10];
listSize = 0;
}

//设置更大的容量
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;
}

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