Java Collections ArrayList array Detailed analysis of the principle and source _ a little class (multi-shore College)

arraylist source code analysis

  1. Array Introduction

    An array is a data structure is the basic structure of many programming languages ​​have built-in array.

    It will be divided in java when creating an array in memory a contiguous memory, and when data will be entered when data is stored in contiguous order in this memory. When the data needs to be read in the array, the array need to provide an index, and the index array will take out the data in memory, the program returns to the reader. In Java, not all data can be stored in an array, only the same type of data can only be stored in the array together.

    img

    Because the data is stored in the array sequentially stored in memory for storing data is continuous, so he is characterized by addressing the data easier to read, insert and delete more difficult .

  2. arraylist source code analysis

    • Construction method

      public ArrayList(int initialCapacity) {
          if (initialCapacity > 0) {
              // 创建指定长度的object数组
              this.elementData = new Object[initialCapacity];
          } else if (initialCapacity == 0) {
              // 空数组
              this.elementData = EMPTY_ELEMENTDATA;
          } else {
              throw new IllegalArgumentException("Illegal Capacity: "+
                                                 initialCapacity);
          }
      }
      
      // 空参构造方法
      public ArrayList() {
          this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
      }
      
      public ArrayList(Collection<? extends E> c) {
          elementData = c.toArray();
          if ((size = elementData.length) != 0) {
              // c.toArray might (incorrectly) not return Object[] (see 6260652)
              if (elementData.getClass() != Object[].class)
                  elementData = Arrays.copyOf(elementData, size, Object[].class);
          } else {
              // replace with empty array.
              this.elementData = EMPTY_ELEMENTDATA;
          }
      }
    • Insert data

      public boolean add(E e) {
          // 检测是否需要扩容
          ensureCapacityInternal(size + 1);  // Increments modCount!!
          // 数组赋值
          elementData[size++] = e;
          return true;
      }
      
      public void add(int index, E element) {
          // 判断index是否越界
          rangeCheckForAdd(index);
          // 检测是否需要扩容
          ensureCapacityInternal(size + 1);  // Increments modCount!!
          // 将index之后的所有元素向后移动一位
          System.arraycopy(elementData, index, elementData, index + 1,
                           size - index);
          // 将index位置覆盖新值
          elementData[index] = element;
          size++;
      }
      
      // 扩容的入口方法
      private void ensureCapacityInternal(int minCapacity) {
          ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
      }
      
      // 计算最小容量
      private static int calculateCapacity(Object[] elementData, int minCapacity) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
              // 默认容量10
              return Math.max(DEFAULT_CAPACITY, minCapacity);
          }
          return minCapacity;
      }
      
      private void ensureExplicitCapacity(int minCapacity) {
          modCount++;
      
          // overflow-conscious code
          // 是否满足扩容的条件 最小容量 - object数组的长度
          if (minCapacity - elementData.length > 0)
              grow(minCapacity);
      }
      // 数组扩容方法
      private void grow(int minCapacity) {
          // overflow-conscious code
          // 当前数组长度
          int oldCapacity = elementData.length;
          // 新的数组容量 = 老容量 + 老容量/2 (1.5倍)
          // oldCapacity = 10
          // oldCapacity >> 1 
          //  0000 1010 >> 1 
          //  0000 0101 = 5
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity < 0)
              newCapacity = minCapacity;
          if (newCapacity - MAX_ARRAY_SIZE > 0)
              newCapacity = hugeCapacity(minCapacity);
          // minCapacity is usually close to size, so this is a win:
          elementData = Arrays.copyOf(elementData, newCapacity);
      }
    • Delete Method

      // 指定位置删除
      public E remove(int index) {
          // 检测index值,IndexOutOfBoundsException
          rangeCheck(index);
      
          modCount++;
          // 返回要删除的元素
          E oldValue = elementData(index);
         // 将index+1及之后的元素向前移动一位,覆盖被删除的值
          int numMoved = size - index - 1;
          if (numMoved > 0)
              System.arraycopy(elementData, index+1, elementData, index,
                               numMoved);
          // 将最后一个位置的元素清空
          elementData[--size] = null; // clear to let GC do its work
      
          return oldValue;
      }
      
      // 指定元素删除
      public boolean remove(Object o) {
          // 判断元素是否为null
          if (o == null) {
              for (int index = 0; index < size; index++)
                  if (elementData[index] == null) {
                      fastRemove(index);
                      return true;
                  }
          } else {
              for (int index = 0; index < size; index++)
                  if (o.equals(elementData[index])) {
                      fastRemove(index);
                      return true;
                  }
          }
          // 如果没有匹配元素,返回false
          return false;
      }
      
      // 快速删除,没有检测index下标
      private void fastRemove(int index) {
          modCount++;
          int numMoved = size - index - 1;
          if (numMoved > 0)
              System.arraycopy(elementData, index+1, elementData, index,
                               numMoved);
          elementData[--size] = null; // clear to let GC do its work
      }
    • Traversal

      ArrayList<Integer> list = new ArrayList();
      
      list.add(10);
      list.add(11);
      list.add(12);
      list.add(13);
      list.add(15);
      
      for (Integer num : list) {
          if (num == 12) {
              list.remove(num);
          }
      }
      // 报错
      // Exception in thread "main" java.util.ConcurrentModificationException
      
      final void checkForComodification() {
          // 不相等就报异常
          if (modCount != expectedModCount)
              throw new ConcurrentModificationException();
      }
      
      // 解决删除异常问题
      Iterator<Integer> it = list.iterator();
      while(it.hasNext()){
          Integer num = it.next();
          if (num == 12) {
              // 使用迭代器的删除方法
              it.remove();
          }
      }
      public void remove() {
          if (lastRet < 0)
              throw new IllegalStateException();
          checkForComodification();
      
          try {
              ArrayList.this.remove(lastRet);
              cursor = lastRet;
              lastRet = -1;
              // 修改expectedModCount和当前modCount一样
              expectedModCount = modCount;
          } catch (IndexOutOfBoundsException ex) {
              throw new ConcurrentModificationException();
          }
      }

    Video Tutorial: http://www.yidiankt.com/

    Little public classroom No.
    Public concern number - free access to core knowledge [JAVA]! !
    QQ discussion group: 706 564 342
    Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yidiankt/p/11458052.html