我正在范 Derby 尔特大学独立学习 CS251。这项工作是为字符数组编写一个外观包装器并使其可调整大小。但是,有一个方法 prune() ,我不明白它应该做什么。你们能解释一下这个方法的含义吗?因为我没上过那所学校,所以我不能问任何人。
这里是该 Java 代码的一个节点。
private class Node {
/**
* Value stored in the Node.
*/
// TODO - you fill in here
/**
* Reference to the next node in the list.
*/
// TODO - you fill in here
/**
* Default constructor (no op).
*/
Node() {
}
/**
* Construct a Node from a @a prev Node.
*/
Node(Node prev) {
// TODO - you fill in here
}
/**
* Construct a Node from a @a value and a @a prev Node.
*/
Node(char value, Node prev) {
// TODO - you fill in here
}
/**
* Ensure all subsequent nodes are properly deallocated.
*/
void prune() {
// TODO - you fill in here
// Leaving the list fully linked could *potentially* cause
// a pathological performance issue for the garbage
// collector.
}
最佳答案
我提供的答案很简单,但我希望从概念上解释您需要什么。在Java中,对象保存在堆上。当垃圾收集发生时,不再被垃圾收集根直接或间接引用的对象将从堆中删除。
如果一个对象仍然被垃圾收集根引用,则它不会被垃圾收集。 prune 方法需要做的就是将当前节点之后的每个节点的 prev 和 next 值设置为 null。这将允许垃圾收集器从堆中删除这些对象。
关于Java : prune method for Java linkedlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39194795/