Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use ArrayList of type char as method's argument

Tags:

java

generics

Cannot define ArrayList<char> as argument of validate. Why it cannot be done? When trying ArrayList<?> it works. Why? Should ArrayList<?> be used 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

like image 573
J.Olufsen Avatar asked May 07 '12 13:05

J.Olufsen


People also ask

How do you declare an ArrayList of a character in Java?

You cannot create an ArrayList of primitive types like int , char etc. You need to use boxed types like Integer , Character , Boolean etc. Java ArrayList is not synchronized. If multiple threads try to modify an ArrayList at the same time, then the final outcome will be non-deterministic.

Can ArrayList store char?

ArrayList cannot hold primitive data types such as int, double, char, and long.

Why ArrayList Cannot use primitives?

ArrayList accepts only reference types as its element, not primitive datatypes. When trying to do so it produces a compile time error.

How do you pass an ArrayList return in Java?

If your intent is to just pass an ArrayList to a method that is inside of Hand , do the following: Change the method's name to something else (that doesn't start with a capital), and get rid of the new keyword in the main .


1 Answers

public boolean validate(List<Character> args){ ... }

It has to be the wrapper type - Character - List<Character>. You can't use generics with primitive types.

like image 57
Bozho Avatar answered Oct 02 '22 21:10

Bozho