簡體   English   中英

查找素數並使用它創建數組時,java.lang.ArrayIndexOutOfBoundsException

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

目標 -找到所有primeNumbers並使用它創建數組完成 -創建方法primeReturner-如果數字質數則返回true-

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

創建帶有質數的數字的方法

    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++;
            }
    }
}

問題-我在創建數組時遇到一些錯誤-數組0中的某些項目及其確定...有時返回錯誤...

-我的方法simpleArray有什么問題?


一點點修改代碼-現在它可以識別所有素數並使用它創建數組,但是在數組中添加第100個項目后,我得到了錯誤

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;
}

和主要功能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();
    }
    }
}

結果創建了所有素數為succsesfull的數組 在此處輸入圖片說明 但是在打印此數組期間我遇到java.lang.ArrayIndexOutOfBoundsException錯誤 ...

如何解決這個錯誤?

您在if(primeReturner(i)) == true)塊中執行i++ 這是for循環i++補充。 您很可能會收到ArrayIndexOutOfBoundsException因為您的i索引將達到大於100的值,這是您的數組最大大小。 if塊中刪除該i++

該錯誤是由於您使用相同的變量在數組中建立索引(即第n個素數的索引)以及素數的值而引起的。 因此,如果索引為非素數,則在simpleArray構造的數組將為零;如果索引為素數,則該值為i 如果需要一個完全打包的數組,則需要第二個索引:

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++;
    }
}

有關一個的素數的討論,請參見Wikipedia

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

這就是問題所在。從代碼中刪除i ++

所以在這里:

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

查找錯誤-需要通過添加限制來停止與i的循環因此,最終代碼為

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]);
    }
}

認為'i <524'不是最好的變體,以后會嘗試找到更好的東西=)感謝所有人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM