我正在为学校编写一个程序,以减少声音文件中的噪音。 到目前为止,我已经编写了这段代码,我认为在一组之前需要 n 个样本,之后需要 n 个样本,然后对两个样本进行平均。我的问题是,每次运行第二个 for 循环时,我都会得到一个示例outofboundException。我猜这意味着它找不到我要求它寻找的样本,但我不明白为什么。
for (int s = 0; s<= aSound.getNumSamples(); s++){
for ( int i=0; i<=level ; i++ ) {
nSamp = aSound.getSample(i);
sSize = nSamp.getValue();
total=total + sSize;
}
for (int j = 0; j >= -level; j--){
sSize2 = aSound.getSample(j).getValue();
total1 = total1 + sSize2;
}
avg = (total1 + total) / level*2;
for (int i = 0; i <= level*2+1; i++){
result.getSample(i).setValue(avg);
}
}
return aSound;
}
每次运行此行时我都会收到错误,但我不明白为什么。有什么帮助吗?谢谢
sSize2 = aSound.getSample(j).getValue();
最佳答案
level
的值是多少?我假设它是积极的,在这种情况下
for (int j = 0; j >= -level; j--)
循环遍历j
的负值,负索引通常是无效的。这就是为什么您会遇到索引越界异常。
如果这不能解决您的问题,您应该发布更多详细信息,例如 aSound
是什么类型的对象。
关于java: 样本越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170732/