创建方法类以从通用数组列表中删除元素

时间:2017-02-13 23:22:48

标签: java generics arraylist

我目前正在学习如何操作通用数组列表。我的教授提供了以下程序的框架,作为一种练习方式,我试图填写方法。但是,我遇到了问题:

public E remove(int index) {
return this.remove((Integer)index); //This is an attempt
}//end remove

在运行时抛出StackOverFlow异常。 下面我有完整的代码(不包括接口和驱动程序):

package array;    
public class DHArrayList<E> implements BareArray<E>{
private int arraySize;   // size is an indication of position in array
private int capacity;
private E[] myArray;
private static final int INITIAL_CAPACITY = 10;
//Once you have created an ArrayList, you can ignore the capacity in all programming that follows

public DHArrayList(){
    capacity = INITIAL_CAPACITY; 
    /*INITIAL_CAPACITY is the number of items that ArrayList will allocate to 
    begin with as the internal storage of items.*/
    arraySize = 0;
}//end default constructor

public DHArrayList(int capacity){
    this.capacity = capacity;
    this.arraySize = 0; //size denotes array indices that are used
    myArray = (E[]) new Object[this.capacity]; 
}//end constructor with parameter

public void add(E a) { //default, will add a value to the end of the list.
       if(arraySize < capacity){ //which entails that there exists space
           //size value gives the index of first free location
           myArray[arraySize] = a; 
           arraySize++;   //updates size
       }//end if
       else{
           System.out.println("Array full. Reallocating . . .");
           this.reallocate();   //Change capacity of array
           this.add(a);
       }//end else       
}//end add

private void reallocate(){ // doubles size of array
    this.capacity *= 2;
    //new array, doubled capacity
    E[] newArray = (E[])new Object[this.capacity]; 
    for(int i = 0; i < this.arraySize; i++){
        newArray[i] = myArray[i]; // reload values
    }//end for

    //Reassigns the myArray pointer to the newArray reference point.
    this.myArray = newArray;
}//end reallocate

public void add(int index, E a) {
    if(index < 0 || index > arraySize){
        System.out.println("Invalid index."); 
        return;
    }//end if

    /*Reusable code from the add method above, 
        else-IF index is at end of list.*/   
    else if(index==arraySize){
        this.add(a);
    }//end else if

    else{
        // Ensure there is space, then move elements and insert.
        if(this.capacity == this.arraySize) {
            this.reallocate();
        }//end if

        //move data
        for (int i = arraySize; i > index; i--){
            this.myArray[i] = this.myArray[i-1]; //shifts to right.
        }//end for

        //Insert data into specified index
        this.myArray[index] = a;
        arraySize++;
    }//end else
}

public E remove(int index) {
    return this.remove((Integer)index); 
}//end remove

public E get(int index) {
    return myArray[index];
}//end get

public void set(int index, E a){ 
}//end set

public int getSize() {
    return 0;
}//end getSize

public int indexOf(E a) {
    return 0;
}//end indexOf

public void display(){
   System.out.println("The contents of the array are ");
   for (int i = 0; i < arraySize; i++) {
       System.out.print(this.myArray[i] +", ");
    }//end for
}//end display
}//end DHArrayList

1 个答案:

答案 0 :(得分:0)

public E remove(int index) {
  return this.remove((Integer)index); // This is an attempt
}

你在这里得到的是无限recursion。您的盒装类Integer会自动取消装箱到int并最终得到无限调用自身的方法(直到它耗尽堆栈内存,这就是您获得StackOverflowException的原因)

Remove方法必须与add方法相似(代码老虎机)正好相反:)