简体   繁体   English

查找素数并使用它创建数组时,java.lang.ArrayIndexOutOfBoundsException

[英]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 - 目标 -找到所有primeNumbers并使用它创建数组完成 -创建方法primeReturner-如果数字质数则返回true-

    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... 问题-我在创建数组时遇到一些错误-数组0中的某些项目及其确定...有时返回错误...

Questing - what wrong with my method simpleArray? -我的方法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 一点点修改代码-现在它可以识别所有素数并使用它创建数组,但是在数组中添加第100个项目后,我得到了错误

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 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 结果创建了所有素数为succsesfull的数组 在此处输入图片说明 but during printing this array i get java.lang.ArrayIndexOutOfBoundsException errors ... 但是在打印此数组期间我遇到java.lang.ArrayIndexOutOfBoundsException错误 ...

How to resolve this error? 如何解决这个错误?

You do i++ in your if(primeReturner(i)) == true) block. 您在if(primeReturner(i)) == true)块中执行i++ This is in addition to the for loop i++ . 这是for循环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. 您很可能会收到ArrayIndexOutOfBoundsException因为您的i索引将达到大于100的值,这是您的数组最大大小。 Remove that i++ in the if block. if块中删除该i++

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. 该错误是由于您使用相同的变量在数组中建立索引(即第n个素数的索引)以及素数的值而引起的。 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. 因此,如果索引为非素数,则在simpleArray构造的数组将为零;如果索引为素数,则该值为i 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 . 有关一个的素数的讨论,请参见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 这就是问题所在。从代码中删除i ++

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 查找错误-需要通过添加限制来停止与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]);
    }
}

Think 'i<524' not the best variant, later will try to find somthing better =) thanks to all. 认为'i <524'不是最好的变体,以后会尝试找到更好的东西=)感谢所有人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 排序数组时出现java.lang.ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException while sorting an array array(java.lang.ArrayIndexOutOfBoundsException:-1) - array (java.lang.ArrayIndexOutOfBoundsException:-1) 从JDBC ResultSet创建数组时,java.lang.ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException when creating array from JDBC ResultSet 创建数组给我java.lang.ArrayIndexOutOfBoundsException - Creating an array is giving me java.lang.ArrayIndexOutOfBoundsException java-不使用数组时出现java.lang.ArrayIndexOutOfBoundsException错误 - java - java.lang.ArrayIndexOutOfBoundsException error while not using an array 查找相邻矩阵:Java.lang.ArrayIndexOutOfBoundsException - Finding adjacent matrix: Java.lang.ArrayIndexOutOfBoundsException [JAVA] Array java.lang.ArrayIndexOutOfBoundsException:0错误 - [JAVA]Array java.lang.ArrayIndexOutOfBoundsException: 0 error 序列化时 JsonMappingException :(是 java.lang.ArrayIndexOutOfBoundsException) - JsonMappingException while serializing : (was java.lang.ArrayIndexOutOfBoundsException) java.lang.ArrayIndexOutOfBoundsException:5个循环 - java.lang.ArrayIndexOutOfBoundsException: 5 WHILE LOOP java.lang.ArrayIndexOutOfBoundsException:while循环中的3 - java.lang.ArrayIndexOutOfBoundsException: 3 in a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM