views:

84

answers:

3

Hi,

As we know ArrayList increases its size by 50% when elements are added(100% incase of Vector).Where can we find the implementation for this behavior?

Thx

+4  A: 

Where can we find the implementation for this behavior?

In the source code. You can find the source code for the Sun Java class libraries in the "src.zip" file in your Sun JDK installation. The sources for OpenJDK 6 and OpenJDK 7 are also available for download via the OpenJDK Project page. For other Java implementations, look on the web or consult the documentation.

Beware - not all Java class libraries implement these classes the same way. So for example, looking at the Apache Harmony project sources or the GNU Classpath project sources won't tell you how the Sun JDK class libraries work.

Stephen C
A: 

.. in the implementation of ArrayList#add !?

Here's an implementation:

public boolean add(E object) {
  if (lastIndex == array.length) {
     growAtEnd(1);
  }
  array[lastIndex++] = object;
  modCount++;
  return true;
}

It calls growAtEnd and inside this method we find the snippet:

} else {
  int increment = size / 2;
  if (required > increment) {
    increment = required;
  }
  if (increment < 12) {
    increment = 12;
  }
  E[] newArray = newElementArray(size + increment);
  if (size > 0) {
    System.arraycopy(array, firstIndex, newArray, 0, size);
    firstIndex = 0;
    lastIndex = size;
  }
  array = newArray;

.. where the increment is set to 50% of the actual size of the current list.


docjar contains the sourcecode of the Apache harmony project, Apaches open source Java SE 6 platform. There a lot of different Java implementations around and it is not guaranteed that each and every implementation shows the exact same behaviour (like increasing size in steps of 50%) as long as it is not documented/required in the interface to this method.

Andreas_D
this implementation is different: http://developer.classpath.org/doc/java/util/ArrayList-source.html
RC
Not like that Andreas..Initially ArrayList will have some default capacity(for vector it is 10).If there is no room for a new element which is added later then it will increase it by 50%.
JavaUser
@RC..So what is the conclusion?
JavaUser
... sorry guys, I was still editing and improving. Initialliy I was mislead by the term 'growAtEnd(1)' and thought - without looking deeper - that it would increase the size by one.
Andreas_D
@JavaUser - neither of the sources linked to by @Andreas_D or @RC are the Sun sources. The Sun sources will include a Sun Microsystems copyright.
Stephen C
+4  A: 

In ArrayList:

public void ensureCapacity(int minCapacity) {
  modCount++;
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    Object oldData[] = elementData;
    int newCapacity = (oldCapacity * 3)/2 + 1;
    if (newCapacity < minCapacity)
      newCapacity = minCapacity;
    // minCapacity is usually close to size, so this is a win:
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

and Vector:

private void ensureCapacityHelper(int minCapacity) {
  int oldCapacity = elementData.length;
  if (minCapacity > oldCapacity) {
    Object[] oldData = elementData;
    int newCapacity = (capacityIncrement > 0) ?
        (oldCapacity + capacityIncrement) : (oldCapacity * 2);
    if (newCapacity < minCapacity) {
      newCapacity = minCapacity;
    }
    elementData = Arrays.copyOf(elementData, newCapacity);
  }
}

Note: capacityIncrement defaults to 0 unless set otherwise so the default behaviour of a Vector is to double every time the backing array needs to be expanded but if you set capacityIncrement then it will be incremented by that instead.

Also in all cases (for ArrayList and Vector) the increase--regardless of what it is--is superseded if the new capacity still isn't large enough, in which case the required capacity is used.

cletus
Proof, cant argue with code...
jjnguy
Note that this has been completely rewritten for JDK 7: http://cr.openjdk.java.net/~martin/webrevs/openjdk7/ArrayResize/
Kevin Bourrillion