Stack&Vector

    xiaoxiao2022-06-30  105

    Stack

    栈,继承了Vector

    /** * 取出并删除栈顶的元素 * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E pop() /** * 把一个元素压入栈顶 和Vector的 addElement 是一个效果 * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) /** * 去除栈顶元素 但不删除 * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E peek() /** * 找到元素从栈顶往下的第几个,如果不存在返回-1 * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) /** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; }

    Vector

    Vector更像是一个大小可变的数组,有对应的下标。有类似于HashMap的扩容机制。 Vector 是矢量队列,它是JDK1.0版本添加的类。继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口。 Vector 继承了AbstractList,实现了List;所以,它是一个队列,支持相关的添加、删除、修改、遍历等功能。 Vector 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。 Vector 实现了Cloneable接口,即实现clone()函数。它能被克隆。 Vector有容量的概念,容量是1024但数据只有1000,就不能取10001的数据,会报错。容量不等于实际元素个数

    和ArrayList不同,Vector中的操作是线程安全的。

    Vector共有4个构造函数 // 默认构造函数 默认大小10 Vector() // capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。 Vector(int capacity) // capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。 Vector(int capacity, int capacityIncrement) // 创建一个包含collection的Vector Vector(Collection<? extends E> collection)

    API:

    /** * 把当前的数组里的内容 全部 拷贝到给定数组中 */ public synchronized void copyInto(Object[] anArray) /** * 将当前容量值 设置为实际元素个数 我的理解只能是从大变小 */ public synchronized void trimToSize() /** * 扩容到指定容量 但不能大于Integer.MAX_VALUE */ public synchronized void ensureCapacity(int minCapacity) /** * 如果新的size大于之前的 补充null 如果小于 吧size及之后的元素设为null 容量不变 */ public synchronized void setSize(int newSize) /** * 获取容量 */ public synchronized int capacity() /** * 获取实际元素个数 */ public synchronized int size() /** * 实际元素是否为0 */ public synchronized boolean isEmpty() /** * Enumeration<Integer> elements = vector.elements(); while (elements.hasMoreElements()) { System.out.println(elements.nextElement()); } 遍历的Iterator */ public Enumeration<E> elements() /** * 是否有这个元素 */ public boolean contains(Object o) /** * 元素的index 没有返回-1 */ public int indexOf(Object o) /** * 从index位置开始找 没有返回-1 */ public synchronized int indexOf(Object o, int index) /** * 从后往前找 没有-1 */ public synchronized int lastIndexOf(Object o) /** * 从后面往前第index位置开始找 没有-1 */ public synchronized int lastIndexOf(Object o, int index) /** * 找到下标为index 的元素 */ public synchronized E elementAt(int index) 省略一些简单的 /** * 在指定index下设置新值 并返回旧值 */ public synchronized E set(int index, E element) /** * 移除第一个匹配的元素 */ public boolean remove(Object o) /** * 移除vector中所有不存在于给定集合中的元素 */ public synchronized boolean retainAll(Collection<?> c)

    Vector有elements和Iterator两个遍历的方式,以下是测试1000的数据时每种遍历方式的耗时(遍历时有休眠,不然不能这么慢): elements cost:1880 iterator cost:1720 forEach cost:1678 index cost:1686


    最新回复(0)