List之Stack源码分析
时间:2022-04-28 07:31
源码版本为JDK1.7.0_75。
该类继承自Vector,说明该类是可克隆的、可序列化的,且是同步的。
/** * vector从n-1到0对应栈顶到栈底,因此在搜索时,应使用lastIndexOf方法 * Returns the 1-based position where an object is on this stack. * If the object o occurs as an item in this stack, this * method returns the distance from the top of the stack of the * occurrence nearest the top of the stack; the topmost item on the * stack is considered to be at distance 1. The equals * method is used to compare o to the * items in this stack. * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o);
if (i >= 0) { return size() - i; } return -1; } |