You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何删除Modelio中分类器拥有的所有属性?代码报错求助

解决Modelio删除分类器所有属性的报错问题

问题原因

你执行attributeList.removeAll(attributeList)时触发了ConcurrentModificationException,这是因为EMF的EList在遍历自身执行删除操作时,会检测到集合结构被修改,从而抛出该异常——直接操作原集合的同时遍历它,就会触发这个问题。

可行解决方案

方案1:用副本删除元素

先创建原属性列表的副本,再通过副本删除原集合中的元素:

List<Attribute> attributeCopy = new ArrayList<>(classifier.getOwnedAttribute());
classifier.getOwnedAttribute().removeAll(attributeCopy);

方案2:迭代器安全删除

使用迭代器的remove()方法,这是集合遍历删除的标准安全方式:

Iterator<Attribute> iterator = classifier.getOwnedAttribute().iterator();
while (iterator.hasNext()) {
    iterator.next();
    iterator.remove();
}

方案3:直接清空集合(优先尝试)

部分EMF实现的EList提供了clear()方法,一步清空所有元素,最为简洁:

classifier.getOwnedAttribute().clear();

说明

优先尝试方案3,若Modelio的EList未实现clear()方法,再选用前两种方案,均能避免并发修改异常。

内容的提问来源于stack exchange,提问作者User214536

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 09:44:57