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();
    }

  • 相关阅读:
    [Python] Calculate pi With MonteCarlo
    二次函数闭区间求最小值、、
    [2013.10.30 Luogu OJ P1509]找啊找啊找GF
    IE8下绝对居中的margin:auto兼容问题解决办法
    上传文件过长中间显示省略号
    全选
    往textarea中光标所在位置插入文本
    侧栏悬浮窗
    IE火狐兼容小知识点(即时更新)
    排序、添加、删除、上移、下移功能
  • 原文地址:https://www.cnblogs.com/zmblog/p/8691110.html
Copyright © 2011-2022 走看看