length
是 PriorityQueue
类的一个实例属性,它表示当前队列中元素的数量。
import { PriorityQueue } from 'yuka';
const priorityQueue = new PriorityQueue();
console.log(priorityQueue.length); // 0
length
的值是一个非负整数,表示当前队列中元素的数量。
每当向队列中添加或删除元素时,length
的值都会相应地更新。
如果队列中没有元素,则 length
的值为 0
。
您可以使用 length
属性来检查队列是否为空,例如:
if (priorityQueue.length === 0) {
console.log('队列为空');
} else {
console.log('队列非空');
}
在以下示例中,我们创建了一个 PriorityQueue
对象,并将元素添加到队列中。然后我们使用 length
属性查看队列中的元素数量,并输出到控制台。
import { PriorityQueue } from 'yuka';
const priorityQueue = new PriorityQueue();
priorityQueue.add(1, 1);
priorityQueue.add(2, 2);
priorityQueue.add(3, 3);
console.log(priorityQueue.length); // 3