Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For some quick references of Java complexities:</p> <ul> <li><a href="http://www.coderfriendly.com/wp-content/uploads/2009/05/java_collections_v2.pdf" rel="nofollow">http://www.coderfriendly.com/wp-content/uploads/2009/05/java_collections_v2.pdf</a></li> <li><a href="http://infotechgems.blogspot.com/2011/11/java-collections-performance-time.html" rel="nofollow">http://infotechgems.blogspot.com/2011/11/java-collections-performance-time.html</a></li> </ul> <p>But really, nothing beats looking to the source, which can be found <a href="http://hg.openjdk.java.net/jdk7/jdk7-gate/jdk/file/tip/src/share/classes/java/util/ArrayList.java" rel="nofollow">here</a>.</p> <hr> <p>Regarding the particular question of adding to an ArrayList, for example, we can see:</p> <pre><code>public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } </code></pre> <p>The complexity for this operation clearly depends on <em>ensureCapacityInternal</em>:</p> <pre><code>private void ensureCapacityInternal(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length &gt; 0) grow(minCapacity); } </code></pre> <p>Which depends on <em>grow</em>:</p> <pre><code>private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1); if (newCapacity - minCapacity &lt; 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE &gt; 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } </code></pre> <p>So we can see that the grow operation happens conditionally, and that in many cases, adding will be an O(1) operation. But you can also see from this the details of <em>when</em> the array will grow.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload