数据结构(Weiss Java 书):为什么在 BinaryHeap中分配 Comparable[]?数组而不是 T[]?
我正在学习数据结构课程,我们正在使用 Mark Weiss 的《Java 中的数据结构和算法分析》第二版。在他的 BinaryHeap 实现中,他的构造函数创建一个被转换为 AnyType[] 的 Comparable[] 数组。您知道他为什么这样做而不是仅仅创建一个新的 AnyType[] 吗?
我了解 BinaryHeap 的结构,但我想了解泛型的最新情况。类声明足够简单,确保 AnyType 扩展了与 AnyType 或 AnyType 继承层次结构上的任何超类可比较的类型(如果 AnyType 是类型的子类,并且不需要更改其compareTo 方法即可运行) )。
但是,array = (AnyType[]) new Comparable[capacity + 1 ];
这行对我来说毫无意义。 AnyType 不是已经是 Comparable 了吗?仅仅编写array = new AnyType[capacity + 1];
会有什么后果?
完整的类源代码可以在他的网站上找到,但是这是我关心的部分:
public class BinaryHeap<AnyType extends Comparable<? super AnyType>>
{
private int currentSize; // Number of elements in heap
private AnyType [ ] array; // The heap array
/**
* Construct the binary heap.
* @param capacity the capacity of the binary heap.
*/
public BinaryHeap( int capacity )
{
currentSize = 0;
array = (AnyType[]) new Comparable[ capacity + 1 ];
}
I'm taking a data structures course, and we're using Data Structures and Algorithm Analysis in Java 2nd Edition by Mark Weiss. In his BinaryHeap implementation, his constructor creates a Comparable[] array that is casted to AnyType[]. Do you have any idea as to why he does this instead of just creating a new AnyType[]?
I understand the structure of the BinaryHeap, but I want to be up to speed on generics. The class declaration is straightforward enough, make sure that AnyType extends a type that is Comparable to AnyType or any superclass up AnyType's inheritance hierarchy (in case AnyType is a subclass of a type and doesn't need to change its compareTo method in order to function).
However, the line, array = (AnyType[]) new Comparable[ capacity + 1 ];
, makes no sense to me. Isn't AnyType already a Comparable? What ramifications are there for just writing array = new AnyType[ capacity + 1 ];
?
The full class source can be found on his site, but here are the parts I'm concerned with:
public class BinaryHeap<AnyType extends Comparable<? super AnyType>>
{
private int currentSize; // Number of elements in heap
private AnyType [ ] array; // The heap array
/**
* Construct the binary heap.
* @param capacity the capacity of the binary heap.
*/
public BinaryHeap( int capacity )
{
currentSize = 0;
array = (AnyType[]) new Comparable[ capacity + 1 ];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法创建泛型类型的数组,因为类型信息在运行时不存在。由于
AnyType
扩展了Comparable
,因此这是唯一可以使用的“具体”类型。转换为
AnyType[]
只是为了确保在出现错误时发出编译时警告;该转换不会存在于生成的字节码指令中。同样,array
类变量将是生成的字节码中的Comparable[]
。You can't create arrays of a generic type because the type information doesn't exist at runtime. As
AnyType
extendsComparable
, that is the only 'concrete' type that can be used.The cast to
AnyType[]
is simply to ensure compile-time warnings are given if there's a mistake; that cast won't exist in the resulting bytecode instructions. Similarly, thearray
class variable will be aComparable[]
in the resulting bytecode.Java 使用 类型擦除 来实现泛型,因此在运行时type AnyType 未知,因此您无法创建它们的数组。看看另一个问题。
Java uses Type Erasure to implement Generics so at run time the type AnyType isn't known and therefore you can't create an array of them. Look at this other question.
由于泛型类型擦除,编译器不知道 AnyType 是什么(因此无法创建它的数组)。但我们知道
AnyType
实现了Comparable
,因此创建 Comparables 数组是一个安全的解决方案。Because of generic type erasure, the compiler has has no idea what
AnyType
is (and hence can't create an array of it). But we knowAnyType
implementsComparable
, so creating an array of Comparables is a safe solution.