2017-09-26 12 views
-4

私はjava.lang.IndexOutOfBoundsException: Index: 1288, Size: 1287 を取得し続けるこれは最初のforループのArrayList<Formant> storedを参照しています。私はなぜArrayListの容量が1287に設定されているのか理解していないdp.sizeArrayListが指定した容量に初期化されないのはなぜですか?

誰でも助けてくれますか? 最大ヒープサイズを10Gbに増やしました 私は2048(dpの最大サイズ)に初期容量を設定しようとしました。 関連するコードを以下に示します。

public Formant[] analyzeBuffer(ArrayList<DataPoint> dp) { 
    //declare variables 
    int count1 = 0; 
    int count2 = 0; 
    ArrayList<DataPoint> stored = new ArrayList<>(dp.size()); 
    Formant[] buffForm = new Formant[12]; 
    //f = new Formant(greatest); 

    //control for infinit loop 
    //while loop checks if datapoint is above threshhold, finds the largest number, and removes all datapoints within a given formant 
    while (!(dp.isEmpty())) { 

     //checks if data point is above threshold, if yes: stores data point in new arraylist 
     for (DataPoint td : dp) { 
      if (td.returnFreq() > threshold) { 
       stored.add(count1, td); 
       count1++; 

      } 
      //checks if data point is the largest number 
      if (td.returnFreq() > greatest) { 
       greatest = td.returnFreq(); 
       f = new Formant(greatest); 
      } 
     } 
     //only removes data points that are formants 
     //removes all data points within a given formant 
     if (f.isFormant) { 
      buffForm[count2] = f; 
      count2++; 
      for (int k = 0; k < stored.size(); k++) { 
       if (stored.get(k).returnFreq() <= (f.determineFormant() + 150) && stored.get(k).returnFreq() >= (f.determineFormant() - 150)) { 
        stored.remove(k); 

       } 
      } 

     } 
     //if freqeuncy is not formant remove all instances of that data point 
     else{ 
      buffForm[count2] = f; 
      count2++; 
      for (int k = 0; k < stored.size(); k++) { 
       if (stored.get(k).returnFreq() == f.freq) { 
        stored.remove(k); 

       } 
      } 

     } 
    } 
    return buffForm; 
} 

答えて

1
stored.remove(k); 

あなただけstoredを縮小しないので、より大きなインデックスは、もは​​や有効です。

ループを逆方向に実行する必要があります。削除によって移動したインデックスを決して使用しないようにする必要があります。

2

ArrayListの容量は、そのサイズとは異なります。そのサイズは "これに含まれる要素の数"ですが、容量は "ArrayListの内部配列のサイズを変更する前にいくつの要素を入れることができますか"です。

List#add(int idx, E element)は、指定されたインデックスの要素が追加されますが、それはそのリストのサイズ(ない容量)十分な大きさが必要です。

[スロー] IndexOutOfBoundsExceptionを - インデックスが範囲(index < 0 || index > size())

外の場合
関連する問題