JDK12中创建泛型ArrayList的语法是什么?

huangapple go评论36阅读模式
英文:

What is the Syntax for creating Generic Arraylist in JDK12?

问题

Currently facing issue as "The type ArrayList is not generic; it cannot be parameterized with arguments " with the below Syntax

ArrayList<String> ls= new ArrayList<String>();

If I try to use non-generic type syntax, then it's giving me an error while calling the add method that add is not defined with the below code

ArrayList ls= new ArrayList();
ls.add("ABC");
英文:

Currently facing issue as "The type ArrayList is not generic; it cannot be parameterized with arguments <String>" with the below Syntax

ArrayList&lt;String&gt; ls= new ArrayList&lt;String&gt;();

If i try to use non generic type syntax , then it's giving me error while calling add method that add<String > is not defined with the below code

ArrayList ls= new ArrayList();
ls.add(&quot;ABC&quot;);

答案1

得分: 1

检查您的导入语句,确保您正在使用正确的 ArrayList,如此处所示。

您的代码:

ArrayList<String> ls = new ArrayList<String>();

是声明 ArrayList 的有效语法。

另外,正如@Louis Wasserman在评论中提到的,确保您没有任何冲突的用户定义 ArrayList 类。

泛型代码:

ArrayList ls = new ArrayList();
ls.add("ABC");

没有太多用途,您应该始终为您的数组列表指定一个类型,如此处所解释的那样。

英文:

Check your import statements to be sure that you are using the proper ArrayList as shown here.

Your code:

ArrayList&lt;String&gt; ls = new ArrayList&lt;String&gt;();

Is valid syntax for declaring an ArrayList.

Additionally, as @Louis Wasserman mentioned in a comment, be sure that you do not have any conflicting user-defined ArrayList classes.

The generic code:

ArrayList ls= new ArrayList();
ls.add(&quot;ABC&quot;);

Does not have many uses, you should always have a type for your array list as explained here.

huangapple
  • 本文由 发表于 2020年7月28日 03:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63122424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定