What is the difference between removeAllElements () and clear () for DefaultListModel?

What is the difference between removeAllElements()and clear()method DefaultListModelin java swing?

java docs for DefaultListModel says: -

public void clear ()

Removes all items from this list. The list will be empty after this call (if only this excludes).

and

public void removeAllElements ()

Removes all components from this list and sets its size to zero.

So, both basically remove all the items from the list, so what's the difference? How to decide when to use?

+3
source share
1 answer

They are both the same.

DefaultListModel Vector .
clear() , Vector , API Collection.

1.3 Collections API , Vector , List.

, , , .

Java:

/**
 * Removes all components from this list and sets its size to zero.
 * <blockquote>
 * <b>Note:</b> Although this method is not deprecated, the preferred
 *    method to use is <code>clear</code>, which implements the 
 *    <code>List</code> interface defined in the 1.2 Collections framework.
 * </blockquote>
 *
 * @see #clear()
 * @see Vector#removeAllElements()
 */
public void removeAllElements() {

        int index1 = delegate.size()-1;
        delegate.removeAllElements();

        if (index1 >= 0) {
            fireIntervalRemoved(this, 0, index1);
        }

}
+4

All Articles