zoukankan      html  css  js  c++  java
  • vector

    vector和arrayList一样底层实现原理也是数组 区别: vector是同步的也就是线程安全的[因为vector里面的某些方法用了synchronized 关键字修饰,比如removeAll(),addAll(),equals(),Iterator()等方法被修饰]

    部分源码

      

    /**
    * Inserts all of the elements in the specified Collection into this
    * Vector at the specified position. Shifts the element currently at
    * that position (if any) and any subsequent elements to the right
    * (increases their indices). The new elements will appear in the Vector
    * in the order that they are returned by the specified Collection's
    * iterator.
    *
    * @param index index at which to insert the first element from the
    * specified collection
    * @param c elements to be inserted into this Vector
    * @return {@code true} if this Vector changed as a result of the call
    * @throws ArrayIndexOutOfBoundsException if the index is out of range
    * ({@code index < 0 || index > size()})
    * @throws NullPointerException if the specified collection is null
    * @since 1.2
    */
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
    modCount++;
    if (index < 0 || index > elementCount)
    throw new ArrayIndexOutOfBoundsException(index);

    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityHelper(elementCount + numNew);

    int numMoved = elementCount - index;
    if (numMoved > 0)
    System.arraycopy(elementData, index, elementData, index + numNew,
    numMoved);

    System.arraycopy(a, 0, elementData, index, numNew);
    elementCount += numNew;
    return numNew != 0;
    }

    /**
    * Compares the specified Object with this Vector for equality. Returns
    * true if and only if the specified Object is also a List, both Lists
    * have the same size, and all corresponding pairs of elements in the two
    * Lists are <em>equal</em>. (Two elements {@code e1} and
    * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
    * e1.equals(e2))}.) In other words, two Lists are defined to be
    * equal if they contain the same elements in the same order.
    *
    * @param o the Object to be compared for equality with this Vector
    * @return true if the specified Object is equal to this Vector
    */
    public synchronized boolean equals(Object o) {
    return super.equals(o);
    }

    /**
    * Returns the hash code value for this Vector.
    */
    public synchronized int hashCode() {
    return super.hashCode();
    }

    /**
    * Returns a string representation of this Vector, containing
    * the String representation of each element.
    */
    public synchronized String toString() {
    return super.toString();
    }

  • 相关阅读:
    ftp 下载最近一小时的文件
    hdu4767 Bell——求第n项贝尔数
    Uva11762 Race to 1——有向无环图&&记忆化搜索
    P3232 [HNOI2013]游走——无向连通图&&高斯消元
    Random Walk——高斯消元法
    B君的历史——复数乘法&&爆搜
    复数快速幂【模板】
    UVa11542Squre——异或方程组&&高斯消元法
    UVa10828 Back to Kernighan-Ritchie——概率转移&&高斯消元法
    高斯消元法【模板】
  • 原文地址:https://www.cnblogs.com/zmblog/p/8691110.html
Copyright © 2011-2022 走看看