1

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 error while calling add method that add is not defined with the below code

ArrayList ls= new ArrayList();
ls.add("ABC");
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
Priya
  • 13
  • 2
  • 3
    These problems make it sound like you're importing the wrong `ArrayList` class, or that you've defined a class named `ArrayList` that is conflicting with the `java.util` one. – Louis Wasserman Jul 27 '20 at 19:44
  • Thanks Louis, I have defined a class named Arraylist which was not allowing me to define generic statement. – Priya Jul 29 '20 at 06:56

1 Answers1

1

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.

NotZack
  • 518
  • 2
  • 9
  • 22