private void changeCapacity(int newCapacity) {
        Element[] newElements = new Element[newCapacity];
        HeadElement newHead = new HeadElement();
        int oldSize = size;
        for (Iterator<E> iter = iterator(); iter.hasNext(); ) {
            Element element = iter.next();
            iter.remove();
            int newSlot = addInternal(element, newElements);
            addToListTail(newHead, newElements, newSlot);
        }
        this.elements = newElements;
        this.head = newHead;
        this.size = oldSize;
    }
Adds a new element to the appropriate place in the elements array.

@param newElement    The new element to add.
@param addElements   The elements array.
@return              The index at which the element was inserted, or INVALID_INDEX
                     if the element could not be inserted.
clients/src/main/java/org/apache/kafka/common/utils/ImplicitLinkedHashCollection.java