5

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

5 Answers 5

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

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

Sign up to request clarification or add additional context in comments.

Comments

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

When use generic in java, you cant use primitive data type, but you can use Character, which is Object that represent primitive char with little overhead in memory.

Comments

1

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

Comments

1

In general, when you'r dealing with a generic object, such as ArrayList<T>, you are required to use objects. The difference between char and Character is that Character is an object, and is permitted to be used inside of a generic object.

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

Comments

1

The wrapper class of char in Java is Character and char should be specified as Character when adding char objects or validating char objects in an ArrayList.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.