简体   繁体   中英

java.lang.ArrayIndexOutOfBoundsException while finding prime Number and creating array with it

Target - find all primeNumbers and create array with it What was done - created method primeReturner - return true if number prime -

    private static boolean primeReturner (int i){
    for (int j=2;j<i; j++){
        if (i%j==0)
            return false;
    }
    return true;
}

created method for creating numbers with prime numbers

    private static void simpleArray()  {
    int []a = new int [100];
    a[0]=1;
    a[1]=2;
    for (int i=2; i<a.length; i++){
        if (primeReturner(i)==true){
            a[i]=i;
            i++;
            }
    }
}

Problem - i get some errors during creating array - some of item from array 0 and some its ok... and some times it return errors...

Questing - what wrong with my method simpleArray?


A little bit modify code - now it recognize all prime numbers and create array with it, but after adding 100th item in to array i get errors

code

private static void simpleArray()  {
    int []a = new int [100];
    a[0]=1;
    a[1]=2;
    int count=2;
    while (count<100)
    for (int i=3; ; ++i){
        if (primeReturner(i)==true){
            a[count]=i;
            count++;
            }
    }
    for (int j=0; j<a.length; j++){
        System.out.print(" " + a[j]);
    }
}
private static boolean primeReturner (int i){
    for (int j=2;j<i; j++){
        if (i%j==0)
            return false;
    }
    return true;
}

and main function public class Exercise_1 { private static int select;

public static void main (String[]args) throws IOException{
    System.out.println("Menu:");
    ....
    System.out.println("Array with simple numbers - enter 7");
    select = getNumber ();

    switch (select){
    .....
    case 7:{
        simpleArray();
    }
    }
}

As result all array with prime number succsesfull created 在此处输入图片说明 but during printing this array i get java.lang.ArrayIndexOutOfBoundsException errors ...

How to resolve this error?

You do i++ in your if(primeReturner(i)) == true) block. This is in addition to the for loop i++ . You'll most likely get an ArrayIndexOutOfBoundsException because your i index will reach a value greater than 100, which is your array max size. Remove that i++ in the if block.

The error is caused by the fact that you use the same variable to index in your array (ie. the index of the n-th prime), and the value of the prime. So the array constructed in simpleArray will have a zero value if the index is non-prime or the value i if it is prime. You need a second index if you want a fully packed array:

private static void simpleArray()  {
    int []a = new int [100];
    a[0] = 2; // by definition 1 is not prime
    int i = 1;
    int possible_prime = 3;
    while (i < a.length) {
        if (primeReturner(possible_prime)) {
            a[i++]=possible_prime;
        }
        possible_prime++;
    }
}

For discussion of the primality of one, see Wikipedia .

 for (int i=2; i<a.length; i++){
        if (primeReturner(i)==true){
            a[i]=i;
            i++;
            }
    }

This is where the problem lies... remove i++ from the code

so here:

for (int i=2; i<a.length; i++){
    if (primeReturner(i)==true){
        a[i]=i;
    }
}

Find error - need to stop loop with i by adding limit So, final code is

private static void simpleArray()  {
    int []a = new int [100];
    a[0]=1;
    a[1]=2;
    int count=2;
    while (count<100){
    for (int i=3;**i<524** ; ++i){
        if (primeReturner(i)==true){
            a[count]=i;
            count++;
            }
        }
    }
    for (count=0; count<a.length; count++){
        System.out.print(" " + a[count]);
    }
}

Think 'i<524' not the best variant, later will try to find somthing better =) thanks to all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM