开发者

Java ArrayList Syntax Error

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?


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>();


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.


Declare your ArrayList using Character:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜