我不斷收到java.lang.IndexOutOfBoundsException: Index: 1288, Size: 1287
這是在第一個for循環中參考ArrayList<Formant> stored
。我不明白爲什麼ArrayList的容量設置爲1287而不是dp.size
爲什麼我的ArrayList未初始化爲我指定的容量?
任何人都可以幫忙嗎? 我已經將最大堆大小增加到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;
}