检查数组是否为空而不获取NullPointerException
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检查数组是否为空而不获取NullPointerException相关的知识,希望对你有一定的参考价值。
当我检查数组是否为空时,出现NullPointerException错误。当数组为空时,它应该返回null,但是我找不到为什么它不起作用。关于我在做什么错的任何提示吗?这是我的完整代码:
import java.lang.reflect.Array;
public class EventPriorityQueue {
class Node {
Event event;
int priority;
Node(Event event, int priority) {
this.event = event;
this.priority = priority;
}
}
private int size;
private Node[] array;
public EventPriorityQueue() {
this.size = 0;
array = new Node[20];
}
public int size() {
return size;
}
public void insert(Event event, int priority) {
if (size == 0) {
array[0] = new Node(event, priority);
size++;
return;
}
if (size == array.length) {
Node[] array2 = new Node[size*2];
System.arraycopy(array, 0, array2, 0, array.length);
array = array2;
}
if (size > 0) {
array[size] = new Node(event, priority);
size++;
percolateUp(size - 1);
}
}
private void percolateUp(int index){
int parentIndex = (index - 1) / 2;
while (array[index].priority < array[parentIndex].priority) {
Node temp = array[parentIndex];
array[parentIndex] = array[index];
array[index] = temp;
index = parentIndex;
parentIndex = (index - 1) / 2;
}
}
public Event peek() {
return array[0].event;
}
public Event poll() {
if (array[0] == null) {
return null;
}
Node deletedElement = array[0];
array[0] = array[size - 1];
array[size - 1] = null;
size--;
percolateDown(0);
return deletedElement.event;
}
private void percolateDown(int index) {
int smallest = index;
int rightChildIndex = (index * 2) + 2;
int leftChildIndex = (index * 2) + 1;
if (array[leftChildIndex] != null) {
if (array[leftChildIndex].priority < array[smallest].priority) {
smallest = leftChildIndex;
}
}
if (array[rightChildIndex] != null) {
if (array[rightChildIndex].priority < array[smallest].priority) {
smallest = rightChildIndex;
}
}
if (smallest != index) {
Node temp = array[index];
array[index] = array[smallest];
array[smallest] = temp;
index = smallest;
percolateDown(smallest);
}
}
public int[] toPrioritiesArray() {
// DO NOT CHANGE THIS FUNCTION
int[] result = new int[this.size];
for (int i = 0; i < this.size; i++) {
result[i] = this.array[i].priority;
}
return result;
}
public static boolean checkHeapProperty(int[] priorities) {
for (int i = 0; i < priorities.length; i++) {
if (2 * i + 1 < priorities.length && priorities[i] > priorities[2 * i + 1]) {
return false;
}
if (2 * i + 2 < priorities.length && priorities[i] > priorities[2 * i + 2]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
// initialize the queue
EventPriorityQueue queue = new EventPriorityQueue();
// add some numbers to the queue
int NUM_EVENTS = 1;
for (int i = NUM_EVENTS; i > 0; i--) {
queue.insert(new Event(i), i);
System.out.println("inserted " + i);
}
// poll everything
for (int i = 1; i <= NUM_EVENTS + 1; i++) {
int next = queue.poll().getTime();
System.out.println("polled " + next);
}
}
}
我认为我的错误在这里:
public Event poll() {
if (array[0] == null) {
return null;
}
任何提示/帮助将不胜感激!
答案
我不太清楚这个问题,但是我认为我看到了这个问题,请尝试-
public Event poll() {
if (array.length == 0) {
return null;
}
希望这会有所帮助!
另一答案
for (int i = 1; i <= NUM_EVENTS + 1; i++) {
int next = queue.poll().getTime();
System.out.println("polled " + next);
}
此循环运行两次。请尝试删除+ 1
。然后将没有NullPointerException。
queue.poll()在第二次迭代中为null。因此,当您对null执行getTime()时。它抛出NullPointerException。
以上是关于检查数组是否为空而不获取NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
当其中一种数据类型可以为空而另一种不能为空时,是不是可以使用 linq 进行匿名连接? [复制]
SpringDataForCassandra:当数据库中的 Int 列设置为空时,实体中的 NullPointerEx