2017-02-13 7 views
-1

現在、ジェネリックアレイリストを操作する方法を学習しています。私の教授は、以下のプログラムのスケルトンを提供しました。練習する方法として、私はその方法を記入しようとしています。しかし、私はセグメントで問題に遭遇しています:ジェネリックアレイリストから要素を削除するメソッドクラスを作成する

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 
+2

あなたは 'remove'メソッドの実際の実装を書いていません。あなたは 'add'メソッドで持っている配列の要素のシフトをすべてやり直さなければならないでしょうし、それを行う正しい方法が' remove'メソッドのためだけであることを正確に理解してください。これは複雑で、少なくとも "add"メソッドと同じくらい複雑です。 –

+1

StackOverflow Exceptionを受け取るのは、スタックスペースがなくなるまで、あなたの 'remove()'メソッドが何度も繰り返し呼び出すためです。さもなければ@LouisWassermanはすでに回答を提供しています – thst

答えて

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

あなたがここに持っていることは無限recursionです。あなたの箱入りクラスIntegerintに自動的にアンボクシング取得し、あなたが無限に自分自身を呼び出す方法で終わる

Remove方法が類似していることがある(それはスタックメモリが不足良くなるまで、それはあなたがStackOverflowExceptionがを得る理由です)(コード万力) addメソッドは正反対です:)

関連する問題