所以我正在创建一个Android应用程序,这个代码抛出"引起:java.lang.OutOfMemoryError:尝试抛出OutOfMemoryError时抛出OutOfMemoryError;没有可用的堆栈跟踪"错误:
public ArrayList<Double> statModa(ArrayList<Double> statRed, ArrayList<Double> uniqueRed) {
ArrayList<Double> result = new ArrayList<>();
int maxFreqIndex = 0;
int maxFreq = Collections.frequency(statRed, uniqueRed.get(0));
for (int i=1; i < statRed.size(); i++){
int currentFreq = Collections.frequency(statRed, statRed.get(i));
if ((currentFreq > maxFreq) || (currentFreq == maxFreq && statRed.get(i) == statRed.get(maxFreqIndex))) {
maxFreqIndex = i;
maxFreq = currentFreq;
}
}
if (maxFreq == statRed.size() / uniqueRed.size())
return result;
if (maxFreqIndex == statRed.size()-1){
result.add(statRed.get(maxFreqIndex));
return result;
}
for (int i=maxFreqIndex+1; i < statRed.size(); i++){
int currentFreq = Collections.frequency(statRed, statRed.get(i));
if (currentFreq != maxFreq) {
result.add(statRed.get(maxFreqIndex));
int tempIndex = maxFreqIndexFinder(statRed, i, maxFreq);
if (tempIndex == maxFreqIndex) {
break;
} else {
i = tempIndex;
maxFreqIndex = tempIndex;
}
} else {
double temp = statRed.get(maxFreqIndex);
int tempTimes = 1;
for (int j=1; i + currentFreq*j - 1 < statRed.size(); j++){
if (currentFreq == Collections.frequency(statRed, statRed.get(i + currentFreq*j - 1))) {
temp += statRed.get(i + currentFreq * j - 1);
tempTimes++;
maxFreqIndex = i + currentFreq * j - 1;
} else {
break;
}
}
result.add(temp/(double)tempTimes);
i = maxFreqIndex;
}
}
return result;
}
public int maxFreqIndexFinder(ArrayList<Double> statRed, int startingIndex, int maxFreq){
int resultIndex = startingIndex - 1;
int count = 0;
double lastFreqValue = 0;
for (int i = startingIndex; i < statRed.size(); i++){
int currentFreq = Collections.frequency(statRed, statRed.get(i));
if (currentFreq == maxFreq) {
count++;
if (count == 1) {
resultIndex = i;
lastFreqValue = statRed.get(i);
} else {
if (lastFreqValue == statRed.get(i)) {
resultIndex = i;
lastFreqValue = statRed.get(i);
} else {
break;
}
}
}
}
return resultIndex;
}
Run Code Online (Sandbox Code Playgroud)
我不太确定究竟是什么导致错误.我怀疑是一个无限循环,但我找不到它,所以我在这里问.在抛出错误之前,它会不断分配内存.
这是控制台日志(如果需要):PasteBin
附加信息:
单击按钮时会运行statModa函数.
红色意味着线.
我运行应用程序并获得错误的值如下:
ArrayList statRed的值为{ 0.1,1,1,2,2,3,3,4,5 5 5}和
ArrayList uniqueRed的值为{ 0.1,1,2,3,4,5 }.
这些也不是给我这个错误的唯一值.如果这个(maxFreq == statRed.size()/ uniqueRed.size())为真则没有错误,如果没有,那么我得到错误.
PS我看到另一个问题有同样的错误,只有一个答案是启用largeHeap,但它对我不起作用.
OOm 是因为该行i = maxFreqIndex;,因为给定的输入maxFreqIndex是 8 并且不会改变,所以你会得到无限循环for (int i=maxFreqIndex+1; i < statRed.size(); i++)
| 归档时间: |
|
| 查看次数: |
7816 次 |
| 最近记录: |