Java 创建方法类以从泛型数组列表中删除元素

Java 创建方法类以从泛型数组列表中删除元素,java,generics,arraylist,Java,Generics,Arraylist,我目前正在学习如何操作通用数组列表。我的教授提供了以下程序的框架,作为实践的一种方式,我正在尝试填写这些方法。但是,我在该细分市场遇到了麻烦: public E remove(int index) { return this.remove((Integer)index); //This is an attempt }//end remove 它在运行时引发StackOverFlow异常。 下面我有完整的代码(不包括接口和驱动程序): 封装阵列; 公共类DHArrayList实现了一个数组{ p

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

public E remove(int index) {
return this.remove((Integer)index); //This is an attempt
}//end remove
它在运行时引发StackOverFlow异常。 下面我有完整的代码(不包括接口和驱动程序):

封装阵列;
公共类DHArrayList实现了一个数组{
private int arraySize;//大小表示数组中的位置
私人int能力;
私有E[]myArray;
专用静态最终int初始容量=10;
//创建ArrayList后,可以忽略接下来所有编程中的容量
公共达赖主义者(){
容量=初始容量;
/*INITIAL_CAPACITY是ArrayList将分配给的项目数
首先,作为项目的内部存储*/
arraySize=0;
}//结束默认构造函数
公共达赖列表(国际容量){
这个。容量=容量;
this.arraySize=0;//size表示使用的数组索引
myArray=(E[])新对象[this.capacity];
}//带参数的结束构造函数
public void add(ea){//default,将在列表末尾添加一个值。
如果(arraySize阵列化){
System.out.println(“无效索引”);
返回;
}//如果结束
/*来自上面add方法的可重用代码,
否则,如果索引位于列表末尾。*/
else if(索引==数组化){
本条增补(a);
}//结束,否则如果
否则{
//确保有空间,然后移动图元并插入。
if(this.capacity==this.arraySize){
这是重新分配();
}//如果结束
//移动数据
对于(int i=arraySize;i>index;i--){
this.myArray[i]=this.myArray[i-1];//向右移动。
}//结束
//将数据插入指定的索引
this.myArray[index]=a;
arraySize++;
}//结束其他
}
公共E删除(int索引){
返回此。删除((整数)索引);
}//端部拆除
公共E-get(int索引){
返回myArray[index];
}//结束
公共空集(int索引,ea){
}//端集
公共int getSize(){
返回0;
}//结束getSize
公共内部索引of(EA){
返回0;
}//末端索引
公共空间显示(){
System.out.println(“数组的内容是”);
for(int i=0;i
你在这里得到的是无限的。您的装箱类
Integer
会自动取消装箱到
int
,最终得到一个无限期调用自身的方法(直到它耗尽堆栈内存,这就是为什么会得到StackOverflowException)


Remove
方法必须与
add
方法相似(code-vise)正好相反:)

您还没有编写任何
Remove
方法的实际实现。您必须对数组中的元素进行所有移位,等等,您在
add
方法中所做的,并准确地找出
remove
方法的正确方法。这将很复杂,至少与
add
方法一样复杂。您会收到StackOverflow异常,因为您的
remove()
方法只是一次又一次地调用自己,直到堆栈空间用完为止。否则@LouisWasserman已经给出了答案
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
}