13

I'm getting an error when declaring this ArrayList as an instance variable in Java.

private ArrayList<char> correctGuesses = new ArrayList<char>();

The error:

Syntax error on token char, Dimension expected after this token

Can I not make ArrayLists with type char?

3 Answers 3

30

You can't use a primitive type, rather you use its Wrapper class.. So instead of char you would have Character

ArrayList<Character> correctGuesses = new ArrayList<Character>();
Sign up to request clarification or add additional context in comments.

Comments

3

You can't use primitives as generic parameters. Instead, you use the wrapped version.
Try this:

private ArrayList<Character> correctGuesses = new ArrayList<Character>();

You can still add char types to it though, because java auto-boxes them. ie

correctGuesses.add((char)63);

would be a legal statement.

Comments

1

Declare your ArrayList using Character:

private ArrayList<Character> correctGuesses = new ArrayList<Character>();

Generics don't work with simple types, they require Objects.

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.