Java – ArrayList warning- warning:[unchecked] does not select Add (E), nor does it run the file. Please help?

I've been trying to make this code feel like an era at this stage It is to calculate prime numbers in a range, and I have written a method to print them Unfortunately, the code will not compile, reference warning:

"Warning:[unchecked] did not choose to call add (E) as a member of the original type java.util.List".

– I learned from Google search that this warning is to not declare what type of value should be in your error, but I have done it only when I try to use it Add() function array list

When I try to run it, I will give a more terrible error "static error: undefined name" primenumbers "

I think I'm blind at this point, although several attempts can't find what I did wrong Can anyone give off some light? Thank you.

import java.util.*;

public class PrimeNumbers { 

    private List listOfPrimeNumbers;  //add a member variable for the ArrayList
    public static void main(String args []){    
      PrimeNumbers primeNumberList = new PrimeNumbers(50);
      primeNumberList.print();  //use our new print method
    }

public PrimeNumbers (int initialCapacity) {
    listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity/2);  //initialCapacity/2 is an easy (if not tight) upper bound
    long numberOfPrimes = 0; //Initialises variable numberOfPrimes to 0
    int start = 2;
    boolean[] isPrimeNumber = new boolean[initialCapacity + 1];

    for (int i=0;i==initialCapacity;i++) {//setting all values in array of booleans to true
    isPrimeNumber[i] = true;
    }
     while (start != initialCapacity)
        {
          if (isPrimeNumber[start])
          {
            listOfPrimeNumbers.add(start);
            //add to array list
            numberOfPrimes++;
            for (int i = start; start < initialCapacity; i+=start)
            {
              isPrimeNumber[i] = false;
            }
          }
          start++;
        }
    }

 public void print()  //add this printout function
 {
     int i = 1; 
     Iterator iterator = listOfPrimeNumbers.listIterator();
     while (iterator.hasNext())
     {
          System.out.println("the " + i + "th prime is: " + iterator.next());
          i++;
     }
     //or just System.out.println(listOfPrimeNumbers);,letting ArrayList's toString do the work.  i think it will be in [a,b,c,..,z] format
 }

 public List getPrimes() {return listOfPrimeNumbers;} //a simple getter isnt a bad idea either,even though we arent using it yet
}

Solution

Change this line

private List listOfPrimeNumbers;  //add a member variable for the ArrayList

to

private List<Integer> listOfPrimeNumbers;  //add a member variable for the ArrayList

This will eliminate the warning

Bonus – you may want to use the enhanced for loop as an alternative to the printing method:

public void print() {
  int i = 1; 
  for (Integer nextPrime:listOfPrimeNumbers) {
      System.out.println("the " + i + "th prime is: " + nextPrime);
      i++;
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>