简体   繁体   中英

Implementation of adding element to ArrayList and removing element from ArrayList

I want to insert a new element into Array List before the position specified by index with add(int i, T t) method in My Array List Class. And also I want to remove all elements from Array List with Clear() method in My Array List Class.

How can I implement this?

My Array List Class:

public class 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(){

}

My Array List Element Class:

public class 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 
} 

}

I will explain you at a conceptual mode...

make a clone of your arraylist that should be empty -> yourArray. clear() is the function that you need to do that.

make a loop

add elements from original arraylist into your clone array until the index that you want to insert. if index=your target cloneArray.add(yourelement); and also add index element... after that continue loop normaly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM