Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "The type ArrayList is not generic" mean?

Tags:

java

I am new to Java and trying to learn the various collections that programmers can use. I imported "java.util" into a scrapbook in Eclipse and inspected the following code.

ArrayList<String> list = new ArrayList<String>();
list.add("test1");
list.add("test2");

I receive this output.

The type ArrayList is not generic; it cannot be parameterized with arguments <String>
Syntax error, parameterized types are only available if source level is 5.0
The type ArrayList is not generic; it cannot be parameterized with arguments <String>
Syntax error, parameterized types are only available if source level is 5.0

What does this error mean? I did not make a generic array list; I made an array list of strings. Furthermore, what is "source level"?

like image 960
John Hoffman Avatar asked May 11 '12 18:05

John Hoffman


People also ask

What is the generic type of ArrayList?

(ArrayList API documentation) ArrayList is a generic container that gives us array-like convenience for accessing elements (the method . get(i) gives us access by index) with linked-list like convenience for adding new elements (the method . add(x) adds a new elment to the end of an ArrayList.

Is an ArrayList a generic collection?

In C#, the ArrayList is a non-generic collection of objects whose size increases dynamically. It is the same as Array except that its size increases dynamically. An ArrayList can be used to add unknown data where you don't know the types and the size of the data.

Is ArrayList a generic interface?

actual type argument from the type of data being passed to the method. The ArrayList class is generic: the definition of the class uses a type parameter for the type of the elements that will be stored. ArrayList<String> specifies a version of the generic ArrayList class that can hold String elements only.


2 Answers

Your Java version in Eclipse is set to 1.4, generics in java were introduced only in Java 5.

Change your JDK to 1.5 or above in eclipse that will resolve this issue.

You can check your JDK by Project - > Java Build Path - > Libraries

If here you see it being Java 1.5 or above then check the compiler Compliance is set to 5 and above.

You can check that Project - > Java Compiler

EDIT:

To add new jdk to Eclipse

Right click on Project - > Java Build Path - > Libraries - > Add Libraries - > JRE System Library - > Installed Libraries - > Add - > Standard VM - > Provide your installation location and press OK

Note in the list of Installed JRE, ensure that you check your Java 7.

like image 91
mprabhat Avatar answered Sep 21 '22 00:09

mprabhat


What comes into my mind:

  • check if the JDK is fully compliant with generics (so that the ArrayList class in your JSE is actually a generic class)
  • check if you don't have another ArrayList which has scope precedence and overrides the standard library definition
like image 45
Jack Avatar answered Sep 21 '22 00:09

Jack