简体   繁体   中英

Prime Numbers Array in Java

I want to return an array that displays all the prime numbers within a certain range from 0 till whatever number I enter.

For the range from 0 to 5 I would like the array returned with [2,3,5] . Within the task I was told by my professor that I should fill the whole array with 0 before replacing those 0 wih prime numbers later.

Currently my code does not return the correct array as I do not seem to access the next location in the array but seem to always assign the value to the first location in the array. My current result array is not [2,3,5] but [5,0,0,0,0] . Any help would be greatly appreciated.

public static int[] generierePrimzahlen(int bis){
int [] myAry = new int[bis];
Arrays.fill(myAry,0);
for(int i=0;i<myAry.length;i++){
      for (int nextprime=1; nextprime < bis; nextprime++){
          int counter = 0;
         // System.out.println(nextprime);
          if (istPrimzahl(nextprime)){
              myAry[counter] = nextprime;
              counter++;
          }

      }
     System.out.print(myAry[i]+" ");
  }

return myAry;


}

PS: I have a functioning method (istPrimzahl), which checks if a certain number is a prime number or not.

The problem is that your counter is in the wrong scope. so instead of incrementing. on every iteration of the first for loop, you declare a new counter. so that it is 0 at the time u assign the prime number to the array.

public static int[] generierePrimzahlen(int bis){
int [] myAry = new int[bis];
// Arrays.fill(myAry,0);    // no need, this is already been done at initialization
for(int i=0;i<myAry.length;i++){
  int counter = 0;

         // adding <= 'nextprime <= bis;' to check also the last number in the range
      for (int nextprime=1; nextprime <= bis; nextprime++){
         // int counter = 0; wrong scope
         // System.out.println(nextprime);
          if (istPrimzahl(nextprime)){
              myAry[counter] = nextprime;
              counter++;
          }

   }
    if(myAry[0] != 0)    // to get rid of displaying Zeros
       System.out.print(myAry[i]+" ");
  }

return myAry;


}

ArrayList will be a better choice than array. However if using array is another school requirement, then what you have done:

int[] myAry = new int[size];

will already set all elements to zeroes.

There is also no need to use 2 loops for this. Just:

  • Loop through from 1 to n
  • if current number is prime, set it to array of current index
  • idx++

I do not seem to access the next location in the array but seem to always assign the value to the first location in the array.

That is because you are setting your counter variable back to zero in every iteration. You should declare it outside your loop.

Example:

int idx = 0;  //place this outside the loop
for(int i=1; i<=n; i++)
    if(isPrime(i))
        myAry[idx++] = i;

Put below line outside both for loop. That will work. Reason for issue is - you are resetting the counter while entering the for loop.

int counter = 0;

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