protected Object[] elementData;// 用于存放元素的数组protectedint elementCount;// 栈内的有效元素/**
* The amount by which the capacity of the vector is automatically
* incremented when its size becomes greater than its capacity. If
* the capacity increment is less than or equal to zero, the capacity
* of the vector is doubled each time it needs to grow.
*
* @serial
*/protectedint capacityIncrement;
/**
* 先判断栈的大小,若栈为空则抛出异常;否则返回数组最后一个元素(栈顶元素)
*/publicsynchronized E peek(){int len =size();if(len ==0)thrownewEmptyStackException();returnelementAt(len -1);}/**
* Returns the number of components in this vector.
*
* @return the number of components in this vector
*/publicsynchronizedintsize(){return elementCount;}
pop():E
弹出栈顶元素,并返回栈顶元素
/**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the {@code Vector} object).
* @throws EmptyStackException if this stack is empty.
*/publicsynchronized E pop(){
E obj;int len =size();
obj =peek();removeElementAt(len -1);return obj;}/**
* 删除数组最后一个元素
*/publicsynchronizedvoidremoveElementAt(int index){if(index >= elementCount){thrownewArrayIndexOutOfBoundsException(index +" >= "+
elementCount);}elseif(index <0){thrownewArrayIndexOutOfBoundsException(index);}int j = elementCount - index -1;if(j >0){
System.arraycopy(elementData, index +1, elementData, index, j);}
modCount++;
elementCount--;
elementData[elementCount]= null;/* to let gc do its work */}
empty():boolean
判断栈是否为空
/**
* Tests if this stack is empty.判断当前数组大小,即elementCount的值即可
*
* @return {@code true} if and only if this stack contains
* no items; {@code false} otherwise.
*/publicbooleanempty(){returnsize()==0;}publicsynchronizedintsize(){return elementCount;}
search(Object):int
查找栈中的某个元素下标
/**
* lastIndexof方法,数组从后往前找第一个,即距离栈顶最近的第一个元素,距离栈顶的距离+1,栈顶则返回1;没找到则返回-1
*/publicsynchronizedintsearch(Object o){int i =lastIndexOf(o);if(i >=0){returnsize()- i;}return-1;}
privatestaticfinalint DEFAULT_INITIAL_CAPACITY =11;/**
* Priority queue represented as a balanced binary heap: the two
* children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The
* priority queue is ordered by comparator, or by the elements'
* natural ordering, if comparator is null: For each node n in the
* heap and each descendant d of n, n <= d. The element with the
* lowest value is in queue[0], assuming the queue is nonempty.
*/transient Object[] queue;// non-private to simplify nested class access/**
* The number of elements in the priority queue.
*/int size;/**
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/privatefinal Comparator<?super E> comparator;/**
* The number of times this priority queue has been
* <i>structurally modified</i>. See AbstractList for gory details.
*/transientint modCount;// non-private to simplify nested class access
DEFAULT_INITIAL_CAPACITY:队列默认大小,初始化未指定队列大小时用此数
queue:用数组来存储元素,实际上是一个小根堆。最小的元素在 queue[0]
size:队列大小
comparator:用户可自定义比较器
modCount:修改次数
构造方法 Constructor
publicPriorityQueue(){this(DEFAULT_INITIAL_CAPACITY, null);}publicPriorityQueue(int initialCapacity){this(initialCapacity, null);}publicPriorityQueue(Comparator<?super E> comparator){this(DEFAULT_INITIAL_CAPACITY, comparator);}publicPriorityQueue(int initialCapacity,
Comparator<?super E> comparator){// Note: This restriction of at least one is not actually needed,// but continues for 1.5 compatibilityif(initialCapacity <1)thrownewIllegalArgumentException();this.queue =newObject[initialCapacity];this.comparator = comparator;}