tags:

views:

388

answers:

2

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!

+1  A: 

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.

ShabbyDoo
+2  A: 

If all sampleWeights is zero sampleSum will be zero and population.length=0. So when you do population[z] there will be an ArrayIndexOutOfBoundsException.

Daniel Moura
If sampleWeights[i] is zero, there will be no population[z] execution.
Tom Hawtin - tackline
Seems to work. Awaiting conformation.
Henrik P. Hessel
works! thanks daniel!
Henrik P. Hessel
you are right, but if sampleWeight[0]=1 and sampleWeight[1]=-1, sampleSum will be zero and we got an Exception
Daniel Moura