View on GitHub

severalfly 个人博客

severalfly 个人博客,一起分享知识

Download this project as a .zip file Download this project as a tar.gz file

I have found the follow codes, when i reand the source of ArrayList

/**
     * Trims the capacity of this <tt>ArrayList</tt> instance to be the
     * list's current size.  An application can use this operation to minimize
     * the storage of an <tt>ArrayList</tt> instance.
     */
    public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }

this is to minimize the capacity of a Arraylist, in case the memory not enough