问题描述
|
Java swing中
DefaultListModel
的removeAllElements()
和clear()
方法有什么区别?
DefaultListModel的Java文档说:-
公共无效clear()
删除所有
此列表中的元素。该列表将
此调用返回后为空
(除非它引发异常)。
和
公共无效removeAllElements()
从该列表中删除所有组件
并将其大小设置为零。
因此,两者基本上都从列表中删除了所有元素,所以有什么区别?如何决定何时使用哪个?
解决方法
他们都是一样的。
DefaultListModel
在引擎盖下使用a4ѭ。
稍后在重写Vector使其适合Collection API时,添加了clear()方法。
在版本1.3中,“ 5”进入了它的位置,因此将“ 4”重写为适合“ 7”界面。
为了使其向后兼容,他们仅在可用且可能的情况下将调用转发给旧的现有方法。
编辑
从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,index1);
}
}