2014-03-27 126 views
0

我想在Array列表中使用add(int i,T t)方法指定的位置之前,將一個新元素插入到Array List中。而且我想在My Array List Class中使用Clear()方法從Array List中刪除所有元素。將元素添加到ArrayList並從ArrayList中刪除元素的實現

我該如何執行此操作?

我的數組列表類:

公共類MyArrayList {

private int n=0; //initial size of the array list 
private MyArrayListElement<T> firstElement;//the first element of the array list 
//Gets the element at index i 
private MyArrayListElement<T> getElement(int i){ 
    if(firstElement == null) return null; 
    int c = 0; 
    MyArrayListElement<T> x=firstElement; 
    while(x!=null){ 
     if(c==i) return x; 
     x=x.getNext(); 
     c++; 
    } 
    return null; 
} 
//Gets the element value at index i 
public T get(int i){ 
    MyArrayListElement<T> element = getElement(i); 
    if(element!=null) return element.getValue(); 
    return null; 
} 
//Removes the element at index i 
public void remove(int i){ 
    MyArrayListElement<T> x= getElement(i); 
    if(x==null) return; 
    if(x.getPrevious()!=null){ 
     x.getPrevious().setNext(x.getNext()); 
    } 
    if(x.getNext()!=null){ 
     x.getNext().setPrevious(x.getPrevious()); 
    } 
    if(x==firstElement){ 
     firstElement = x.getNext(); 
    } 
    n--; // decrement the size of the array list 
} 
//Adds a new element to the end 
public void add(T t){ 
    MyArrayListElement<T> element = new MyArrayListElement<T>(t); 
    if(firstElement == null){ 
     firstElement = element; 
    }else{ 
     MyArrayListElement<T> lastElement=getElement(n-1); //Get the last element 
     lastElement.setNext(element); //Add new element to the end 
     element.setPrevious(lastElement);//Update previous element 
    } 
    n++; //increment the size 
} 
//Returns the number of elements in the array list 
public int size(){ 
    return n; 
} 
public String toString(){ 
    String str ="{"; 
    for(int i=0;i<n;i++){ 
     str+=get(i); 
     if(i<n-1){ 
      str+=","; 
     } 
    } 
    str+="}"; 
    return str; 
} 

public void add(int index, T t) { 

} 

public void clear(){ 

} 

我的數組列表元素類:

公共類MyArrayListElement {

private T value; // This is the data stored in this element 
private MyArrayListElement <T> next; // the next element 
private MyArrayListElement <T> previous; // the previous element 

//Constructor gets an object of type <T> as an argument 
public MyArrayListElement(T t) { 
    value = t; //stores the object in the instance variable "value" 
} 
public void setValue(T val){ 
    value = val; //change the stored object 
} 
public void setNext(MyArrayListElement <T> n){ 
    next = n; //set the link to the next element 
} 
public MyArrayListElement<T> getNext() { 
    return next; //get the next element 
} 
public T getValue(){ 
    return value; //get the data stored in this element 
} 
public MyArrayListElement <T> getPrevious() { 
    return previous; //get the previous element 
} 
public void setPrevious(MyArrayListElement <T> previous) { 
    this.previous = previous; //set the link to the previous element 
} 

}

+0

這是功課嗎?或者你爲什麼要實現你自己的ArrayList? – keuleJ

+0

如果你真的不想自己做家庭作業,你可以使用任何搜索引擎和搜索雙向鏈表,你會發現算法(在維基百科中有一個僞代碼)。 – StephaneM

+1

這不是一個數組列表,正如其名稱所示,它是由數組支持的。這是一個鏈表。 –

回答

0

我會在概念模式下解釋你...

使你的數組​​列表克隆應該是空的 - > yourArray。 clear()是你需要做的功能。

使循環

添加由原來的ArrayList的元素融入到你的克隆陣列,直到要插入索引。 if index =您的目標cloneArray.add(yourelement);並添加索引元素...之後,繼續循環normaly。

+0

對(int i = 0; i