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"?
(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.
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.
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.
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.
What comes into my mind:
ArrayList
class in your JSE is actually a generic class)ArrayList
which has scope precedence and overrides the standard library definitionIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With