Java ArrayList 类型不是通用的;它不能用参数 <Integer> 参数化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22059271/
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-13 13:17:52  来源:igfitidea点击:

The type ArrayList is not generic; it cannot be parameterized with arguments <Integer>

javagenericsarraylistinteger

提问by Ryel

I am trying to complete a JCF array list, and it compiled just fine 30 minutes ago, but now I am receiving the error "The type ArrayList is not generic; it cannot be parameterized with arguments ". I have tried a few things to figure it out, but I am at a loss. Here is the code:

我正在尝试完成一个 JCF 数组列表,它在 30 分钟前编译得很好,但现在我收到错误“ArrayList 类型不是通用的;它不能用参数参数化”。我已经尝试了一些事情来弄清楚,但我不知所措。这是代码:

import java.util.*;

/**
 * Class to test the java.util.ArrayList class.
 */
public class Main
{

    public static void main(String[] args)
    {
        Main myAppl = new Main();

    }

    public Main()
    {
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        //list creation
        for (int i = 0; i < 10; i++)
            numbers.add((int) (Math.random() * 100));

        System.out.println("List of numbers:");
        System.out.println(numbers);

        Scanner in = new Scanner(System.in);
        System.out.print("Please, enter an int value: ");
        int x = in.nextInt();

        if (numbers.contains(x))
            System.out.println("Found!");
        else
            System.out.println("Not found!");
    }
}

回答by Alex Suo

2 possibilities:

2 种可能性:

  1. You are using some mysterious ArrayList from a 3rd party package rather than java.util.ArrayList; or

  2. Your compiler settings is pre-1.5 or your effective JDK is pre-1.5 so generic wasn't available.

  1. 您正在使用来自 3rd 方包的一些神秘的 ArrayList 而不是 java.util.ArrayList;或者

  2. 您的编译器设置是 1.5 之前的,或者您的有效 JDK 是 1.5 之前的,因此通用不可用。

回答by Elliott Frisch

Assuming you're using (and targeting) a version greater then or equal to 1.5 then you must have a class named ArrayList that shadows the one you want; you should use

假设您正在使用(并定位)一个大于或等于 1.5 的版本,那么您必须有一个名为 ArrayList 的类来隐藏您想要的那个;你应该使用

// If your compiler settings are correct.
java.util.ArrayList<Integer> numbers = new java.util.ArrayList<Integer>();

回答by Abdul Rasheed Mohammed

Try changing your class name to a different one. i.e. other than ArrayList or main.

尝试将您的班级名称更改为不同的名称。即除了 ArrayList 或 main。