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

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

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

I get this conclusion.

 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 create a general list of arrays; I made a list of string arrays. Also, what is the β€œbaseline”?

+6
source share
3 answers

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

Change the JDK to 1.5 or higher in eclipse to solve this problem.

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

If you see that it is Java 1.5 or higher, then check for compiler 5 and higher.

You can check that Project - > Java Compiler

EDIT:

To add a new jdk in Eclipse

Right-click on Project β†’ Java Build Path β†’ Libraries β†’ Add Libraries β†’ JRE Library β†’ Installed Libraries β†’ Add β†’ Standard Virtual Machine β†’ Specify the installation location and click OK

Please note that in the list of installed JREs, make sure you test your Java 7.

+10
source

What comes to mind:

  • check if the JDK is fully compatible with generics (so the ArrayList class in your JSE is actually a generic class)
  • check if you have another ArrayList that takes region priority and overrides the standard library definition
+4
source

Yes, the problem goes to 1.5 and above. It seems that the question is not completely resolved, although I am adding my 2 cents in case anyone comes across this question. This mainly concerns this part of the question:

I did not create a general list of arrays; I made a list of string arrays.

Error message:

> ArrayList type is not common; it cannot be parameterized with arguments <String>

> Syntax error, parameterized types are only available if source level 5.0

What this actually means, since Java 1.5 we can also use type parameters (where parameter values ​​were used in one of them). JDK 1.5 introduces generics that allow us to abstract from types (or parameterized types ).

Class designers can be generic types in a definition. The arrayList implementation will look like this:

 public class ArrayList<E> implements List<E> .... { // Constructor public ArrayList() {...} // Public methods public boolean add(E e) {...} public void add(int index, E element) {...} public boolean addAll(int index, Collection<? extends E> c) {...} public abstract E get(int index) {...} public E remove(int index) {...} ... } 

Where E can be any type, like String or Integer, etc. Therefore, the name is a generic array of List.

Users can be specific in types when creating an object or calling a method that was executed in this example, as shown below:

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

(This was a confusion in the above case, if I am not mistaken :-))

An example of using generics (if necessary):

 // Declaring a DAO layer public interface IMasterAbstractDao<E, I> { public E findById(I id) {...} public void delete(E e) {...} public List<E> findByCriteria(Criterion criterion) {...} } 

Where E is the return type of the object. This can be used for the entire beans model defined in the system, which makes it common.

Hope this helps.

Link: Java Programming Tutorial - General

+1
source

Source: https://habr.com/ru/post/915526/


All Articles