本文介绍了ArrayList警告 - 警告:[未选中]未经检查的调用添加(E),也不会运行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让这段代码适合现阶段的感觉。它是为了计算一个范围内的素数,我写了一个方法来打印它们。不幸的是,代码不会编译,引用警告:

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:

警告:[未选中]未选中调用添加(E)作为原始类型java.util的成员。列表

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

- 我从谷歌搜索中了解到这个警告是因为没有声明你的erray中应该包含哪些类型的值,但是我已经这样做了,而且只有错误似乎是在我尝试在我的数组列表中使用.add()函数时出现的。

--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.

当我尝试运行它时,它给出了一个更可怕的错误
静态错误:未定义的名称'PrimeNumbers'

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
}


推荐答案

更改此行

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

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

这将消除警告。

奖金 - 您可能希望在<$ c中使用增强型循环 $ c> print 方法作为替代方法:

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++;
  }
}

这篇关于ArrayList警告 - 警告:[未选中]未经检查的调用添加(E),也不会运行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:34