Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList warning- warning: [unchecked] unchecked call to add(E), also file will not run

Tags:

I've been trying to get this code to work for what feels like an age at this stage. it is meant to compute prime numbers in a range, and I've written a method to print them. Unfortunately the code will not compile, citing the warning:

"warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List"

--I understand from googling that this warning is for not declaring what types of values should be in your erray, but I have done this, and the error only seems to come about when I try to use the .add() function on my array list.

and when I try to run it it gives a somewhat more scary error of "Static Error: Undefined name 'PrimeNumbers'

I think I've gone code-blind at this point and despite several attempts cannot find out what I am doing wrong.

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
}
like image 550
user476033 Avatar asked Nov 29 '10 12:11

user476033


1 Answers

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 elimiate the warning.


Bonus - you may want to use the enhanced for loop inside the print method as an alternative approach:

public void print() {
  int i = 1; 
  for (Integer nextPrime:listOfPrimeNumbers) {
      System.out.println("the " + i + "th prime is: " + nextPrime);
      i++;
  }
}
like image 102
Andreas Dolk Avatar answered Oct 28 '22 15:10

Andreas Dolk