快速修复 Java 错误 ArrayIndexOutOfBoundsException
我只是盲目地看到解决方案吗?
sampleSum = 0;
for(int x : sampleWeights)
sampleSum += x;
population = new int[sampleSum];
int z = 0;
for(int i = 0; i < nsamples; i++)
for(int j = 0; j < sampleWeights[i]; j++)
{
population[z] = i;
z++;
}
任何帮助表示赞赏! 谢谢!
Am I just to blind to see the solution?
sampleSum = 0;
for(int x : sampleWeights)
sampleSum += x;
population = new int[sampleSum];
int z = 0;
for(int i = 0; i < nsamples; i++)
for(int j = 0; j < sampleWeights[i]; j++)
{
population[z] = i;
z++;
}
Any help is appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果所有的sampleWeights都为零,则sampleSum将为零并且population.length=0。 因此,当您执行population[z] 时,将会出现 ArrayIndexOutOfBoundsException。
If all sampleWeights is zero sampleSum will be zero and population.length=0. So when you do population[z] there will be an ArrayIndexOutOfBoundsException.
我假设你有 N 个整数,N = # Samples x # Weights/Sample(i)?
如果是这种情况,您从哪里获得 SampleWeights 数组(或给定整数自动装箱的列表)?
使用一些 System.out.println 再次运行您的代码以收集调试信息,您很快就会发现问题所在。
I presume that you have N integers, N = # Samples x # weights/Sample(i)?
If this is the case, from where did you get the sampleWeights array (or List given autoboxing of Integers)?
Run your code again with some System.out.println's to gather debug info, and you'll quickly see what's wrong.