gpt4 book ai didi

java - ArrayDeque 类的字符

转载 作者:搜寻专家 更新时间:2023-10-31 00:02:52 26 4
gpt4 key购买 nike

我正在尝试将我用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com