Cant似乎找到了问题,现在我的代码有些失落了……真的可以更好地利用它。真的很感激。
这是错误:
Exception in thread "main" java.lang.NullPointerException
at Heap.<init>(Heap.java:16)
at Heap$1.<init>(Heap.java:131)
at Heap.main(Heap.java:131)
http://codepaste.net/jogkx2
public abstract class Heap {
private List heap_array; // Arraylist containing heap.
private List<ELEM> priority_queue; //ArrayList containing ELEMS.
private int heapsize; // Maximum heapsize of the heap.
private int n; // Number of elements currently in the heap.
private int last_elem = heap_array.size()-1; // last elem in the heap
public Heap(int s, int n) {
heap_array = new ArrayList();
priority_queue = new ArrayList<ELEM>();
s = heapsize;
n = this.n;
}
protected int returnMaxPriority(){
int max = priority_queue.get(priority_queue.size()-1).getLocation();
return max;
}
public void shiftDown(int i) {
int leftChild = leftChild(i);
int rightChild = rightChild(i);
int largest = i;
// Max Heapify
if (leftChild < heap_array.size()
&& !aboveOrEqual(largest, leftChild)) {
largest = leftChild;
}
if (rightChild < heap_array.size()
&& !aboveOrEqual(largest, rightChild)) {
largest = rightChild;
}
if (largest != i) {
switchElems(largest, i);
shiftDown(largest);
}
}
public void insert(Object obj, int priority) {
heap_array.add(obj);
int object_i = 0;
while (object_i > 0 && !aboveOrEqual(parent(object_i), object_i)) {
switchElems(parent(object_i), object_i);
object_i = parent(object_i);
}
// enqueue(object_i, priority);
}
public Object removeLast() {
if (heap_array.size() > 0) {
switchElems(0, last_elem);
Object result = heap_array.remove(last_elem);
shiftDown(0);
return result;
} else {
return null;
}
}
public Object get(int index) {
return heap_array.get(index); //Return object
}
public int heapsize() {
return heap_array.size();
}
protected int parent(int i) {
return (i - 1) / 2;
}
protected void update_N(int n){
n = this.n;
}
protected void update_Size(int size){
heapsize = size;
}
protected int leftChild(int i) {
return 2 * i + 1;
}
protected int rightChild(int i) {
return 2 * i + 2;
}
protected void switchElems(int i, int j) {
Object tmp = heap_array.get(i);
heap_array.set(i, heap_array.get(j));
heap_array.set(j, tmp);
}
public void enqueue(int object_i, int priority) {
ELEM tmp = new ELEM(priority, object_i);
priority_queue.add(object_i, tmp);
}
public int dequeue() {
if (heap_array.size() > 0) {
int max_location = returnMaxPriority();
heap_array.remove(max_location);
return max_location;
}
else{return -1;}
}
public void changeWeight(int object_i, int priority){
ELEM tmp = new ELEM(priority, object_i);
priority_queue.set(object_i, tmp);
}
protected abstract boolean aboveOrEqual(int value1, int value2);
public static void main(String[] args){
Heap h = new Heap(100, 20) {
protected boolean aboveOrEqual(int value1, int value2) {
return ((Integer)get(value1)).intValue()
>= ((Integer)get(value2)).intValue(); //Compares the objects int values.
}
};
System.out.println("Enter a list of numbers, then type quit: ");
for (int i = 0; i < 100; i++) {
h.insert(new Integer((int)(100 * Math.random())), i);
}
}
}
class sortPriorityArray implements Comparator<ELEM> {
@Override
public int compare(ELEM s1, ELEM s2){
int value1 = s1.getPriority();
int value2 = s2.getPriority();
if(value1 < value2){
return 1;
}
else if(value2 > value1){
return -1;
}
return 0;
}
如果您还可以看看我的insert函数和returnmax。 ELEM类包含优先级和位置,因此我可以通过按优先级对priority_queue进行排序,然后以适当的顺序出队列,然后首先使最高优先级出队列。
最佳答案
private int last_elem = heap_array.size()-1; // last elem in the heap
该行在尚未初始化的列表上调用
size()
。关于java - 空指针运行时异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17891941/