Java ArrayList 警告-警告:[unchecked] 未选中的 add(E) 调用,文件也不会运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4303511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:38:20  来源:igfitidea点击:

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

java

提问by user476033

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"

“警告:[unchecked] 未经检查调用 add(E) 作为原始类型 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.

--我从谷歌搜索中了解到,此警告是为了不声明您的错误中应包含哪些类型的值,但我已经这样做了,并且该错误似乎仅在我尝试在我的计算机上使用 .add() 函数时出现数组列表。

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

当我尝试运行它时,它给出了一个更可怕的错误“静态错误:未定义名称‘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
}

采纳答案by Andreas Dolk

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 loopinside the printmethod as an alternative approach:

奖金-你可能要使用增强的for循环的内部print方法作为一种替代的方法:

public void print() {
  int i = 1; 
  for (Integer nextPrime:listOfPrimeNumbers) {
      System.out.println("the " + i + "th prime is: " + nextPrime);
      i++;
  }
}

回答by brain

You've decalred primeNumbers to be an untyped List but then created an ArrayList of Integer. Change the declaration of primeNumbers to:

您已将 primeNumbers 标记为无类型列表,但随后创建了一个 Integer ArrayList。将 primeNumbers 的声明更改为:

private List<Integer> listOfPrimeNumbers; 

The for loop you are using to set all the isPrimeNumber to true doesnt work, the condition should be i<=initialCapacityor even better use:

您用来将所有 isPrimeNumber 设置为 true 的 for 循环不起作用,条件应该是i<=initialCapacity甚至更好地使用:

Arrays.fill(isPrimeNumber, true);

In your print method I wouldnt bother using an iterator and keeping track of the int i, just use a normal for loop.

在您的打印方法中,我不会费心使用迭代器并跟踪 int i,只需使用普通的 for 循环即可。

Without knowing what command you are using to build the code and then try and run it, it is hard to diagnose your runtime error. Make sure your command window is in the same directory as your .class file.

如果不知道您使用什么命令来构建代码,然后尝试运行它,就很难诊断您的运行时错误。确保您的命令窗口与 .class 文件位于同一目录中。