Cannot use an charray ArrayList as a method argument

Unable to define ArrayList<char> as validate argument. Why is this impossible? When trying ArrayList<?> It works. What for? Should I use ArrayList<?> Instead of ArrayList<char> ? What is the difference?

 public boolean validate(ArrayList<char> args){ ... } 

Error: Syntax error on token "char", Dimensions expected after this token

+4
source share
5 answers
 public boolean validate(List<Character> args){ ... } 

It should be a wrapper type - Character - List<Character> . You cannot use generics with primitive types.

+12
source
 public boolean validate(ArrayList<Character> args){ ... } 

When you use generic in java, you cannot use a primitive data type, but you can use Character , which is an object representing a primitive char with little overhead in memory.

+6
source

You can try to do something like this: public boolean validate(ArrayList<Character> args){ ... }

+1
source

In the general case, when you are dealing with a universal object, for example ArrayList<T> , you need to use objects. The difference between char and Character is that Character is an object and is allowed to be used inside a shared object.

For reference, each primitive type has its own wrapper object. You can check it out here .

+1
source

The char wrapper class in Java is a character, and char must be specified as a character when adding char objects or checking char objects in an ArrayList.

 ArrayList<Character>list = new ArrayList<Character>(); 
+1
source

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


All Articles