zoukankan      html  css  js  c++  java
  • Vector

    概述

            Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。 

            由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是快速失败的:如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险。Vector 的 elements 方法返回的 Enumeration 不是 快速失败的。 

     定义

    public class Vector<E>
        extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
    View Code

    字段

    protected Object[] elementData;  元素存在的位置,不能存null
     protected int elementCount;    有效元素个数
    protected int capacityIncrement;     每次扩容要增元素个数
    View Code

    构造函数

    初始容量,扩容时增加数  
      public Vector(int initialCapacity, int capacityIncrement) {
            super();
            if (initialCapacity < 0)
                throw new IllegalArgumentException("Illegal Capacity: "+
                                                   initialCapacity);
            this.elementData = new Object[initialCapacity];
            this.capacityIncrement = capacityIncrement;
        }
        public Vector(int initialCapacity) {
            this(initialCapacity, 0);
        }
    默认构造器
        public Vector() {
            this(10);
        }
    View Code

     Vector 的 elements 方法返回的 Enumeration 不是 快速失败的。

     public Enumeration<E> elements() {
            return new Enumeration<E>() {
                int count = 0;
    
                public boolean hasMoreElements() {
                    return count < elementCount;
                }
    
                public E nextElement() {
                    synchronized (Vector.this) {
                        if (count < elementCount) {
                            return elementData(count++);
                        }
                    }
                    throw new NoSuchElementException("Vector Enumeration");
                }
            };
        }
    View Code

    两种快速失败迭代器

      private class Itr implements Iterator<E> {
            int cursor;       // index of next element to return
            int lastRet = -1; // index of last element returned; -1 if no such
            int expectedModCount = modCount;
    
            public boolean hasNext() {
                // Racy but within spec, since modifications are checked
                // within or after synchronization in next/previous
                return cursor != elementCount;
            }
    
            public E next() {
                synchronized (Vector.this) {
                    checkForComodification();
                    int i = cursor;
                    if (i >= elementCount)
                        throw new NoSuchElementException();
                    cursor = i + 1;
                    return elementData(lastRet = i);
                }
            }
    
            public void remove() {
                if (lastRet == -1)
                    throw new IllegalStateException();
                synchronized (Vector.this) {
                    checkForComodification();
                    Vector.this.remove(lastRet);
                    expectedModCount = modCount;
                }
                cursor = lastRet;
                lastRet = -1;
            }
    
            final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
            }
        }
    
        /**
         * An optimized version of AbstractList.ListItr
         */
        final class ListItr extends Itr implements ListIterator<E> {
            ListItr(int index) {
                super();
                cursor = index;
            }
    
            public boolean hasPrevious() {
                return cursor != 0;
            }
    
            public int nextIndex() {
                return cursor;
            }
    
            public int previousIndex() {
                return cursor - 1;
            }
    
            public E previous() {
                synchronized (Vector.this) {
                    checkForComodification();
                    int i = cursor - 1;
                    if (i < 0)
                        throw new NoSuchElementException();
                    cursor = i;
                    return elementData(lastRet = i);
                }
            }
    
            public void set(E e) {
                if (lastRet == -1)
                    throw new IllegalStateException();
                synchronized (Vector.this) {
                    checkForComodification();
                    Vector.this.set(lastRet, e);
                }
            }
    
            public void add(E e) {
                int i = cursor;
                synchronized (Vector.this) {
                    checkForComodification();
                    Vector.this.add(i, e);
                    expectedModCount = modCount;
                }
                cursor = i + 1;
                lastRet = -1;
            }
        }
    }
    View Code
  • 相关阅读:
    STRIDE威胁分析与DREAD威胁评价
    HashMap 几大问题
    java 集合中的错误检测机制
    科创人·StreamNative翟佳:开源模式价值为王,基础软件的未来在国内社区
    科创人·云柚智能CEO汤峥嵘:价值观一致奠定共事基础,技术创新加速行业变革
    科创人·微软中国CTO韦青:数智时代创业得跳下巨人肩膀,还需掌握基础知识和逻辑能力
    科创人研习社·微智云CEO 张虎:从CTO到创始人关键是扩大视野半径
    科创人·天云数据CEO雷涛:打造正确理解数智的认知体系
    neovim环境与vim简单使用
    MIT6.828——Lab3 PartA(麻省理工操作系统实验)
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7355444.html
Copyright © 2011-2022 走看看