Your answer is one click away!

Chris Nedworth February 2016
### Array index out of bounds in sieve of eratosthenes

I am having trouble with ArrayIndexOutOfBounds. I am new to coding and I don't fully understand this problem and I am unable to fix it. Any help is appreciated.

```
public class Primes {
public static void main(String[] args) {
final int SIZE = 10000;
boolean[] numberIsPrime = new boolean[SIZE];
int rowCounter = 0;
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
for( int index = 2; index <= SIZE; index++) {
if( numberIsPrime[index] = true){
for( int i = index; i <= SIZE; i++){
numberIsPrime[index * i] = false;
}
}
}
for( int index = 1; index <= SIZE; index++){
if( numberIsPrime[index] = true){
System.out.println(index + " ");
rowCounter++;
if( rowCounter == 10){
System.out.println();
}
}
}
}
}
```

You should tag the question with the language you're using, but I'm gonna assume it's Java. The problem is

```
boolean[] numberIsPrime = new boolean[SIZE];
...
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
```

The first line declares numberIsPrime as an array of size 10000. That means you can access `numberIsPrime[0], numberIsPrime[1], ... numberIsPrime[9999]`

. You can't access `numberIsPrime[10000]`

.

Asked in February 2016

Viewed 1,402 times

Voted 4

Answered 1 times

Viewed 1,402 times

Voted 4

Answered 1 times