java - ArrayDeque 类的字符

标签 java c++ generics

我正在尝试将我用 C++ 编写的双括号平衡器移动到 Java。

我试图通过像这样声明字符的 ArrayDeque 来使用 Deque 接口(interface)中的 ArrayDeque 类来实现堆栈:

Deque<char> parens = new ArrayDeque<char>();

编译器在声明中窒息
expected: reference<br/> found: char

我错过了什么?

最佳答案

您不能将原始类型用作泛型参数。您需要相应的对象包装器:

Deque<Character> parens = new ArrayDeque<Character>();

Let's update our Box class to use generics. We'll first create a generic type declaration by changing the code public class Box to public class Box<T>; this introduces one type variable, named T, that can be used anywhere inside the class. This same technique can be applied to interfaces as well. There's nothing particularly complex about this concept. In fact, it's quite similar to what you already know about variables in general. Just think of T as a special kind of variable, whose "value" will be whatever type you pass in; this can be any class type, any interface type, or even another type variable. It just can't be any of the primitive data types. In this context, we also say that T is a formal type parameter of the Box class.

[Source: Java Tutorial : Generics : Generic Types]

参见:

关于java - ArrayDeque 类的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286200/

相关文章:

java - 使用 Spring :message tag inside of form:input tag in Spring MVC

java - Maven 本地依赖

java - 无法在树莓派上运行java程序

c++ - 从 lldb 中制作 C++ 文件

swift - 无法将类型 Node<T> 的值分配给类型 Node<_>?

java - 为什么我不能在内部接口(interface)中使用泛型?

c++ - 段错误(核心已转储)C++ 面向对象编程

c++ - CUDA共享内存原子错误

c++ - 有没有一种通用的方法可以用 SFINAE 否定 decltype 条件?

来自传播运算符的 typescript 联合类型