2016-03-18 33 views
0

雖然改變基於SonarQube建議,我結識下面行的某些代碼:如何增加ArrayList的大小爲100%,就像矢量

  • 自動增加運力的矢量默認數組的大小加倍。當你向ArrayList中插入一個元素時,它會將其數組大小增加50%。

  • 現在我想知道我是否需要更換向量與ArrayList中有代碼的正常執行失敗的機會。

    記住現有的矢量並沒有做任何Thead安全的工作。

    問:

    1. 是ArrayList中有足夠的能力來調整就像載體?

    2. 在除了同步之外的任何情況下,用ArrayList替換Vector是否安全?

    3. 是否有任何確切的置換載體的(不期待線程安全)

    請隨時更新的問題或要求任何東西。

    +0

    你期待什麼故障? – assylias

    +3

    [Vector is broken](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated),你應該**使用'ArrayList',並且如果您需要同步,請使用'synchronizedList'。 – Idos

    +0

    在現有代碼中,向量中添加了URL列表(沒有最大限制)。我害怕如果我使用具有50%增量的ArrayList可能會破壞該功能。 – MonsterJava

    回答

    0

    我沒有看到問題。 Vector和ArrayList的確切性能指標並不相同,但對於大多數實際用途而言,這並不重要。 ArrayList將在需要的時候擴展,比Vector更頻繁(如果你事先沒有告訴它需要的容量)。前進。

    對於您的問題:1.是2.是3.無

    2

    VectorArrayList之間的差別是一些這樣的:

    1. VectorArrayList不同步是同步的。所以,Vector是線程安全的。
    2. Vector速度慢,因爲它是線程安全的。比較ArrayList是非快速的,因爲它是非同步的。
    3. A Vector默認情況下其數組的大小增加了一倍。而當您將一個元素插入ArrayList時,它會將陣列大小增加50%。

      • 的ArrayList:

        /** 
        * Increases the capacity to ensure that it can hold at least the 
        * number of elements specified by the minimum capacity argument. 
        * 
        * @param minCapacity the desired minimum capacity 
        */ 
        private void grow(int minCapacity) { 
            // overflow-conscious code 
            int oldCapacity = elementData.length; 
            int newCapacity = oldCapacity + (oldCapacity >> 1); // 50% 
            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); 
        } 
        
      • 矢量:

        private void grow(int minCapacity) { 
            // overflow-conscious code 
            int oldCapacity = elementData.length; 
            int newCapacity = oldCapacity + ((capacityIncrement > 0) ? 
                   capacityIncrement : oldCapacity); // default 100% 
            if (newCapacity - minCapacity < 0) 
             newCapacity = minCapacity; 
            if (newCapacity - MAX_ARRAY_SIZE > 0) 
             newCapacity = hugeCapacity(minCapacity); 
            elementData = Arrays.copyOf(elementData, newCapacity); 
        } 
        
    4. ArrayList中沒有定義的增量大小。向量定義增量大小。

      /** 
          * The amount by which the capacity of the vector is automatically 
          * incremented when its size becomes greater than its capacity. If 
          * the capacity increment is less than or equal to zero, the capacity 
          * of the vector is doubled each time it needs to grow. 
          * 
          * @serial 
          */ 
          protected int capacityIncrement; 
      

    基於以上:

    1. ArrayList能不能調整就像Vector
    2. ArrayList不是線程安全的。它不能用多個線程直接替代VectorArrayList
    3. 它大部分可以用單線程代替VectorArrayList。由於VectorArrayList聲明:

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