简体   繁体   English

Java ArrayList语法错误

[英]Java ArrayList Syntax Error

I'm getting an error when declaring this ArrayList as an instance variable in Java. 我在将这个ArrayList声明为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? 我可以不使用char类型创建ArrayLists吗?

不能使用基本类型,而你使用它的包装类..所以不是char ,你将有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. 你仍然可以添加char类型,因为java会自动添加它们。 ie

correctGuesses.add((char)63);

would be a legal statement. 将是一份法律声明。

Declare your ArrayList using Character : 使用Character声明您的ArrayList

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

Generics don't work with simple types, they require Objects. 泛型不适用于简单类型,它们需要对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM