【问题标题】:Implementation of adding element to ArrayList and removing element from ArrayListArrayList添加元素和ArrayList移除元素的实现
【发布时间】:2014-05-06 09:58:21
【问题描述】:

我想在我的数组列表类中使用 add(int i, T t) 方法在索引指定的位置之前将一个新元素插入到数组列表中。而且我还想在我的数组列表类中使用 Clear() 方法从数组列表中删除所有元素。

我该如何实现呢?

我的数组列表类:

公共类 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 
} 

}

【问题讨论】:

  • 这是作业吗?或者你为什么要实现自己的 ArrayList?
  • 如果你真的不想自己做作业,你可以使用任何搜索引擎搜索双向链表,你会找到算法(在伪代码中维基百科有一个)。
  • 这不是一个数组列表,顾名思义,它由一个数组支持。这是一个链表。
  • 首先 - 这是一个链表而不是数组列表......但简而言之,在设置时不需要太多努力(因为它看起来是某种功课),你应该使用给定索引调用getElement并在i'th元素和next之间插入新元素(创建新元素,将其设置为iths next并将新元素设置为i'ths next),删除单个元素将相似且清晰 - 您必须遍历整个集合和对 prev / next 的 null 引用
  • 这是作业。是的。但是,这部分非常艰难。我没有问所有的家庭作业。只是需要帮助。

标签: java arrays arraylist


【解决方案1】:

我会以概念模式向你解释...

克隆一个应该为空的数组列表 -> yourArray.clear() 是您需要执行此操作的函数。

做一个循环

将原始数组列表中的元素添加到您的克隆数组中,直到您要插入的索引。 if index=你的目标 cloneArray.add(yourelement);并添加索引元素...之后继续正常循环。

【讨论】:

  • 对(int i=0;i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多