• 线性存储结构-ArrayList、Vector


    ArrayList:采用数组的内部构建形式,也就是顺序存储模式。当新增一个对象时,如果当前长度超过预设,会使用System.arraycopy(定义一个更长的数组进行复制处理),这个时候开销比较大。

    System.arraycopy,数组长度小于32的采用的是java层的遍历处理,长数组采用的是native层的处理
      /**
      * The byte array length threshold below which to use a Java
      * (non-native) version of arraycopy() instead of the native
      * version. See b/7103825.
      */
      private static final int ARRAYCOPY_SHORT_BYTE_ARRAY_THRESHOLD = 32;
    
      /**
      * The byte[] specialized version of arraycopy().
      *
      * @hide internal use only
      */
      public static void arraycopy(byte[] src, int srcPos, byte[] dst, int dstPos, int length) {
      if (src == null) {
      throw new NullPointerException("src == null");
      }
      if (dst == null) {
      throw new NullPointerException("dst == null");
      }
      if (srcPos < 0 || dstPos < 0 || length < 0 ||
      srcPos > src.length - length || dstPos > dst.length - length) {
      throw new ArrayIndexOutOfBoundsException(
      "src.length=" + src.length + " srcPos=" + srcPos +
      " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length);
      }
      if (length <= ARRAYCOPY_SHORT_BYTE_ARRAY_THRESHOLD) {
      // Copy byte by byte for shorter arrays.
      if (src == dst && srcPos < dstPos && dstPos < srcPos + length) {
      // Copy backward (to avoid overwriting elements before
      // they are copied in case of an overlap on the same
      // array.)
      for (int i = length - 1; i >= 0; --i) {
      dst[dstPos + i] = src[srcPos + i];
      }
      } else {
      // Copy forward.
      for (int i = 0; i < length; ++i) {
      dst[dstPos + i] = src[srcPos + i];
      }
      }
      } else {
      // Call the native version for longer arrays.
      arraycopyByteUnchecked(src, srcPos, dst, dstPos, length);
      }
      }

    新增时间复杂度o(n)

      @Override public boolean add(E object) {
      Object[] a = array;
      int s = size;
      if (s == a.length) {
      Object[] newArray = new Object[s +
      (s < (MIN_CAPACITY_INCREMENT / 2) ?
      MIN_CAPACITY_INCREMENT : s >> 1)];
      System.arraycopy(a, 0, newArray, 0, s);
      array = a = newArray;
      }
      a[s] = object;
      size = s + 1;
      modCount++;
      return true;
      }

    删除时间复杂度o(n)

      @Override public E remove(int index) {
      Object[] a = array;
      int s = size;
      if (index >= s) {
      throwIndexOutOfBoundsException(index, s);
      }
      @SuppressWarnings("unchecked") E result = (E) a[index];
      System.arraycopy(a, index + 1, a, index, --s - index);
      a[s] = null; // Prevent memory leak
      size = s;
      modCount++;
      return result;
      }

    获取数据,采用的是数据下标的形式,具有高的查询效率时间复杂度o(1)

     @SuppressWarnings("unchecked") @Override public E get(int index) {
      if (index >= size) {
      throwIndexOutOfBoundsException(index, size);
      }
      return (E) array[index];
      }
    ArrayList,是一个顺序存储结构,内部采用数组的形式构建。能够高效的随机访问元素,时间复杂度为0(1),但对于快速插入及删除元素,效率较低,时间复杂度为0(n)。
    另外Vector跟ArrayList都是实现了List接口。Vector同样也是顺序的存储结构。两者主要的区别如下:
    (1)Vector是线程安全,ArrayList是非线程安全
    (2)2者扩容机制不一样,Vector可以设置capacityIncrement增长因子,ArrayList大于MIN_CAPACITY_INCREMENT/2时增长0.5倍数。




  • 相关阅读:
    326. Power of Three
    python实现Excel删除特定行、拷贝指定行操作
    283. Move Zeroes
    268. Missing Number
    263. Ugly Number
    258. Add Digits
    257. Binary Tree Paths
    二叉树的创建,递归前序、中序、后序遍历以及队列实现层遍历
    242. Valid Anagram
    237. Delete Node in a Linked List
  • 原文地址:https://www.cnblogs.com/huangjunbin/p/6062848.html
Copyright © 2020-2023  润新知