繁体   English   中英

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

[英]Creating a method class to remove element from generic array list

我目前正在学习如何操作通用数组列表。 我的教授提供了以下程序的框架,作为练习的方式,我正在尝试填写方法。 但是,我在该细分市场遇到了麻烦:

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
public E remove(int index) {
  return this.remove((Integer)index); // This is an attempt
}

你在这里得到的是一个无限递归 您的装箱类Integer会自动拆箱为int并且您最终会无限期地调用自己的方法(直到它用完堆栈内存,这就是您得到 StackOverflowException 的原因)

Remove方法必须相似(代码虎钳)才能add相反的方法:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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