【发布时间】:2012-10-01 16:12:04
【问题描述】:
我迷失在代码中。我正在尝试将返回值添加到 ArrayList 中。返回的值被打印出来,我尝试将其转换为可以将其添加到 ArrayList。但似乎没有任何效果。
我在public void getHeap() 中,想从public double remove() 中检索return 值以添加到一个ArrayList 中。它不断告诉我source not found。有什么帮助吗?
谢谢!
public class MinHeap<E extends Comparable<E>> {
List<E> h = new ArrayList<E>();
ArrayList<Double> arrayPostingsList = new ArrayList<Double>();
public void getHeap() {
MinHeap<Double> heap = new MinHeap<Double>(new Double[]{0.5015530788463572, 0.5962770626486013, 0.4182157748994399});
ArrayList<Double> newArray = new ArrayList<Double>();
System.out.println();
while (!heap.isEmpty()) {
System.out.println(heap.remove());
newArray.add(heap.remove());
}
}
public double remove() {
E removedNode = h.get(0);
E lastNode = h.remove(h.size() - 1);
percolateDown(0, lastNode);
return (Double) removedNode;
}
public MinHeap() {
}
public MinHeap(E[] keys) {
for (E key : keys) {
h.add(key);
}
for (int k = h.size() / 2 - 1; k >= 0; k--) {
percolateDown(k, h.get(k));
}
}
public void add(E node) {
h.add(null);
int k = h.size() - 1;
while (k > 0) {
int parent = (k - 1) / 2;
E p = h.get(parent);
if (node.compareTo(p) >= 0) {
break;
}
h.set(k, p);
k = parent;
}
h.set(k, node);
}
public E min() {
return h.get(0);
}
public boolean isEmpty() {
return h.isEmpty();
}
void percolateDown(int k, E node) {
//....
}
}
【问题讨论】:
-
请编辑您的帖子并包含错误的堆栈跟踪;还要在代码中指出引发错误的行。
-
假设有两个
remove方法吗?因为你的例子不会编译 -
请发帖Short, Self Contained, Correct (Compilable), Example。您发布的代码无法编译。您定义了 2 个删除方法。
-
请同时包含您的错误消息和堆栈跟踪
-
我删除了一个
remove方法。