英文:
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
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
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<String> ls= new ArrayList<String>();
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("ABC");
答案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<String> ls = new ArrayList<String>();
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("ABC");
Does not have many uses, you should always have a type for your array list as explained here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论